简体   繁体   English

如何从特定文件夹的ftp​​下载文件

[英]How to download files from ftp from particular folder

I create a windows form to download files from ftp from particular folder. 我创建一个Windows窗体以从特定文件夹的ftp​​下载文件。

The user put the ftp details with username and password and folder name from which file will be download all the files. 用户在ftp详细信息中输入用户名和密码以及文件夹名称,将从该文件中下载所有文件。 This will set by user one time and the all file from ftp describe folder will download everyday. 这将由用户设置一次,并且ftp describe文件夹中的所有文件每天都会下载。
Example on FTP Folder Name is MyFolder where a.docx, b.docx etc it will download a.docx, b.docx everyday not other folder data need to download. FTP文件夹名称的示例是MyFolder,其中a.docx,b.docx等将每天下载a.docx,b.docx,而无需下载其他文件夹数据。

For download and list of file I use below function. 对于下载和文件列表,我使用以下功能。 Can you please tell me what I am doing mistake or how can I do this . 你能告诉我我在做什么错还是我该怎么做。

 private void downloadFileFromFTP()
 {
    try
    {
        string[] files = GetFileList();
        foreach (string file in files)
        {
            Download(file);
        }
    }
    catch (Exception ex)
    {
    }
}

For Get The List of file 用于获取文件列表

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;
    }
}

download the file form the folder 从文件夹下载文件

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");
    }
}

I think 3 lines of your Download method have to be corrected as follows: 我认为您的Download方法的3行必须按如下所示进行更正:

1. 1。

string uri = "ftp://" + txtFtpAddress.Text.Trim() + "/" + "txtlodername.Text" + "/" + file;

should be: 应该:

string uri = "ftp://" + txtFtpAddress.Text.Trim() + "/" + txtFTPFolderName.Text.Trim() + "/" + file;


2. 2。

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(txtFtpAddress.Text + "/" + txtFTPFolderName + "/" + file));

should be: 应该:

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


3. 3。

FileStream writeStream = new FileStream("D\\Temp"  + file, FileMode.Create);  

should be: 应该:

FileStream writeStream = new FileStream("D:\\Temp\\"  + file, FileMode.Create);  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM