2012-01-18 9 views
0

C#アプリケーションでFTPを使用してディレクトリをダウンロードしようとしています。私は基本的にリモートディレクトリを取って、その内容とその内容をローカルディレクトリに移動する必要があります。C# - ダウンロードディレクトリ - FTP

ここに私が現在使っている機能と、ログの出力とエラーの内容を示します。私が参照していたサンプルは、おそらく、ディレクトリではないファイルを取得するためのものです、そして:

private void Download(string file, string destination) 
    {      
     try 
     { 
      string getDir = "ftp://" + ftpServerIP + ftpPath + file + "/"; 
      string putDir = destination + "\\" + file; 

      Debug.WriteLine("GET: " + getDir); 
      Debug.WriteLine("PUT: " + putDir); 

      FtpWebRequest reqFTP;     

      reqFTP = (FtpWebRequest)FtpWebRequest.Create 
       (new Uri(getDir)); 

      reqFTP.Credentials = new NetworkCredential(ftpUserID, 
                 ftpPassword); 
      reqFTP.UseBinary = true; 

      reqFTP.KeepAlive = false;     
      reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;         
      reqFTP.Proxy = null;     
      reqFTP.UsePassive = false; 
      FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); 
      Stream responseStream = response.GetResponseStream(); 
      FileStream writeStream = new FileStream(putDir, FileMode.Create);     
      int Length = 2048; 
      Byte[] buffer = new Byte[Length]; 
      int bytesRead = responseStream.Read(buffer, 0, Length);    
      while (bytesRead > 0) 
      { 
       writeStream.Write(buffer, 0, bytesRead); 
       bytesRead = responseStream.Read(buffer, 0, Length); 
      }     
      writeStream.Close(); 
      response.Close(); 
     } 
     catch (WebException wEx) 
     { 
      MessageBox.Show(wEx.Message, "Download Error"); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message, "Download Error"); 
     } 
    } 

デバッグ:

GET: ftp://I.P.ADDR/SOME_DIR.com/members/forms/THE_FOLDER_TO_GET/ 
PUT: C:\Users\Public\Documents\iMacros\Macros\THE_FOLDER_TO_WRITE 
A first chance exception of type 'System.Net.WebException' occurred in System.dll 

メッセージボックス出力:

The requested URI is invalid for this FTP command. 
+0

宛先ディレクトリはFTPサーバー上に存在しますか?使用しているユーザーに適切な権限がありますか? – Oded

+0

@Oded:ユーザーが行います。私は以前にディレクトリをリストし、オプションとしてディレクトリのCheckedListBoxを作成しました。 FileZillaを使って手動でデータを取得することもできます。 – Josh

答えて

3

GETDIRの末尾にスラッシュディレクトリを指しています - あなたはmgetを使って、 "/ *"で終わるようなパスを渡せますか?

+0

私はあなたが 'mget'の意味を正確には分かりませんが、要求の最後に*を付けても問題は解決しません。 – Josh

+1

申し訳ありませんが、mgetが複数のファイルを取得するコマンドであるコマンドラインFTPを考えていました。 WebRequestMethods.Ftpには同等のものがないようです。 ListDirectoryメソッドを使用してリスティングを取得し、それをループするのはどうですか? – upsidedowncreature

+0

私はあなたの答えを受け入れました。なぜなら、 'mget'は私にディレクトリの内容をリストアップし、一度に1つずつループさせるというアイデアを与えたからです。 – Josh

関連する問題