【问题标题】:How to Download Numerous Files from FTP using C#?如何使用 C# 从 FTP 下载大量文件?
【发布时间】:2013-08-07 19:14:07
【问题描述】:

我需要按计划的时间间隔运行一个控制台应用程序,该应用程序只需要从 FTP 站点下载 .pgp 文件。必须下载 FTP 中的任何 pgp 文件。我找到了获取 FTP 目录列表的示例代码,并将其写在这里:

FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://ourftpserver");
        req.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

        req.Credentials = new NetworkCredential("user", "pass");

        FtpWebResponse response = (FtpWebResponse)req.GetResponse();

        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        Console.WriteLine(reader.ReadToEnd());

        Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription);

        reader.Close();
        response.Close();

我必须怎么做才能从目录列表中下载所有 .pgp 类型的文件并将它们保存在我们服务器上的本地目录中?

【问题讨论】:

  • 解析响应并使用循环。
  • SLaks 你能用代码示例详细说明一下吗?

标签: c# ftpwebrequest


【解决方案1】:

FtpWebRequestFtpWebResponse 对象确实设计为发出单个请求(即下载单个文件等)

您正在寻找一个 FTP 客户端。 .NET Framework 中没有,但有一个免费的,System.Net.FtpClient,显然工作得很好。

【讨论】:

  • 非常感谢!我在这个网站、MSDN 和其他网站上阅读了很多帖子,但您的信息是直接且中肯的。非常感激。现在就下载这个:)
【解决方案2】:

有很好的库可以使用https://sshnet.codeplex.com/ 代码sn-p: 您需要将要下载文件的文件夹路径传递为 localFilesPath,并将要下载文件的 Ftp 文件夹路径传递为 remoteFTPPath。

public static void DownloadFilesFromFTP(string localFilesPath, string remoteFTPPath)
        {
            using (var sftp = new SftpClient(Settings.Default.FTPHost, Settings.Default.FTPUsername, Settings.Default.FTPPassword))
            {
                sftp.Connect();
                sftp.ChangeDirectory(remoteFTPPath);
                var ftpFiles = sftp.ListDirectory(remoteFTPPath, null);
                StringBuilder filePath = new StringBuilder();
                foreach (var fileName in ftpFiles)
                {

                    filePath.Append(localFilesPath).Append(fileName.Name);
                    string e = Path.GetExtension(filePath.ToString());
                    if (e == ".csv")
                    {
                        using (var file = File.OpenWrite(filePath.ToString()))
                        {
                            sftp.DownloadFile(fileName.FullName, file, null);
                            sftp.Delete(fileName.FullName);
                        }
                    }
                    filePath.Clear();
                }
                sftp.Disconnect();
            }
        }

【讨论】:

  • 根据这个答案开始使用这个,非常好!
【解决方案3】:

从 ftp 下载文件的代码。

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://127.0.0.0/my.txt");
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.Credentials = new NetworkCredential("userid", "pasword");
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        Stream responseStream = response.GetResponseStream();
        FileStream file = File.Create(@c:\temp\my.txt);
        byte[] buffer = new byte[32 * 1024];
        int read;
        //reader.Read(

        while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0)
        {
            file.Write(buffer, 0, read);
        }

        file.Close();
        responseStream.Close();
        response.Close();

【讨论】:

    【解决方案4】:

    Ultimate FTP 可以帮助你。 下面的代码 sn -p 证明了这一点:

    using ComponentPro.IO;
    using ComponentPro.Net;
    
    ...
    
    // Create a new instance.
    Ftp client = new Ftp();
    
    // Connect to the FTP server.
    client.Connect("myserver");
    
    // Authenticate.
    client.Authenticate("userName", "password");
    
    // ...
    
    // Get all directories, subdirectories, and files from remote folder '/myfolder' to 'c:\myfolder'.
    client.DownloadFiles("/myfolder", "c:\\myfolder");
    
    // Get all directories, subdirectories, and files that match the specified search pattern from remote folder '/myfolder2' to 'c:\myfolder2'.
    client.DownloadFiles("/myfolder2", "c:\\myfolder2", "*.pgp");
    
    // or you can simply put wildcard masks in the source path, our component will automatically parse it.
    // download all *.pgp files from remote folder '/myfolder2' to local folder 'c:\myfolder2'.
    client.DownloadFiles("/myfolder2/*.pgp", "c:\\myfolder2");
    
    // Download *.pgp files from remote folder '/myfolder2' to local folder 'c:\myfolder2'.
    client.DownloadFiles("/myfolder2/*.pgp", "c:\\myfolder2");
    
    // Get files in the folder '/myfolder2' only.
    TransferOptions opt = new TransferOptions(true, RecursionMode.None, false, (SearchCondition)null, FileExistsResolveAction.Overwrite, SymlinksResolveAction.Skip);
    client.DownloadFiles("/myfolder2", "c:\\myfolder2", opt);
    
    // ...
    
    // Disconnect.
    client.Disconnect();
    

    http://www.componentpro.com/doc/ftp 有更多示例。

    【讨论】:

    • 请注意,这是一个付费图书馆,而不是一个很棒的图书馆。会避免。有很多不错的免费解决方案!
    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多