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.
宛先ディレクトリはFTPサーバー上に存在しますか?使用しているユーザーに適切な権限がありますか? – Oded
@Oded:ユーザーが行います。私は以前にディレクトリをリストし、オプションとしてディレクトリのCheckedListBoxを作成しました。 FileZillaを使って手動でデータを取得することもできます。 – Josh