【问题标题】:How to download files from ftp from particular folder如何从特定文件夹的 ftp 下载文件
【发布时间】:2015-10-31 09:03:04
【问题描述】:

我创建了一个 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");
    }
}

【问题讨论】:

    标签: c# .net winforms streamreader


    【解决方案1】:

    我认为你的 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);  
    

    【讨论】:

    • 感谢您的快速响应我正在做这些更改我的问题是如何首先列出给定目录中的所有文件,其次我只想下载用户描述的 ftp 中的 txtFTPFolderName 文件
    • 我想下载特定文件夹的文件。所以首先我如何从特定文件夹example MyFolder contains a.docx 读取所有文件我只需要从 ftp 下载 a.docx 而不是其他文件
    • @A.Goutam 您的GetFileList 方法看起来不错,但Uri 必须是new Uri( "ftp://" + txtftpAddress.Text + "/" + "MyFolder" + "/")。然后让用户选择列出的文件。 ListBox控件应该是最好的选择。
    • @jtmt 我尝试了我的 GetFileList 返回文件列表,我希望我可以获取我的MyFolder 目录中的文件列表。当前代码返回The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
    • @jtmt GetFileList 返回我的 ftp 上所有文件的列表。这是谜题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-23
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多