![](/img/trans.png)
[英]How can I download all files from a ftp folder with c# winforms?
[英]How to download files from ftp from particular folder
我創建一個Windows窗體以從特定文件夾的ftp下載文件。
用戶在ftp詳細信息中輸入用戶名和密碼以及文件夾名稱,將從該文件中下載所有文件。 這將由用戶設置一次,並且ftp describe文件夾中的所有文件每天都會下載。
FTP文件夾名稱的示例是MyFolder,其中a.docx,b.docx等將每天下載a.docx,b.docx,而無需下載其他文件夾數據。
對於下載和文件列表,我使用以下功能。 你能告訴我我在做什么錯還是我該怎么做。
private void downloadFileFromFTP()
{
try
{
string[] files = GetFileList();
foreach (string file in files)
{
Download(file);
}
}
catch (Exception ex)
{
}
}
用於獲取文件列表
public string[] GetFileList()
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
WebResponse response = null;
StreamReader reader = null;
try
{
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri( "ftp://" + txtftpAddress.Text + "/")); //txtFtpAddress.Text + "/" + txtFTPFolderName + "/" + file
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential("UserNm", "passwd");
reqFTP.Method = WebRequestMethods.Ftp .ListDirectory;
reqFTP.Proxy = null;
reqFTP.KeepAlive = false;
reqFTP.UsePassive = false;
response = reqFTP.GetResponse();
reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
// to remove the trailing '\n'
result.Remove(result.ToString().LastIndexOf('\n'), 1);
return result.ToString().Split('\n');
}
catch (Exception ex)
{
if (reader != null)
{
reader.Close();
}
if (response != null)
{
response.Close();
}
downloadFiles = null;
return downloadFiles;
}
}
從文件夾下載文件
private void Download(string file)
{
try
{
string uri = "ftp://" + txtFtpAddress.Text.Trim() + "/" + "txtlodername.Text" + "/" + file;
Uri serverUri = new Uri(uri);
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return;
}
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(txtFtpAddress.Text + "/" + txtFTPFolderName + "/" + file));
reqFTP.Credentials = new NetworkCredential("UserName", "mypass");
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Proxy = null;
reqFTP.UsePassive = false;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream responseStream = response.GetResponseStream();
FileStream writeStream = new FileStream("D\\Temp" + file, 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");
}
}
我認為您的Download
方法的3行必須按如下所示進行更正:
1。
string uri = "ftp://" + txtFtpAddress.Text.Trim() + "/" + "txtlodername.Text" + "/" + file;
應該:
string uri = "ftp://" + txtFtpAddress.Text.Trim() + "/" + txtFTPFolderName.Text.Trim() + "/" + file;
2。
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(txtFtpAddress.Text + "/" + txtFTPFolderName + "/" + file));
應該:
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
3。
FileStream writeStream = new FileStream("D\\Temp" + file, FileMode.Create);
應該:
FileStream writeStream = new FileStream("D:\\Temp\\" + file, FileMode.Create);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.