使用 FTP 和 C# 下载所有文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/913312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:41:10  来源:igfitidea点击:

Downloading all files using FTP and C#

c#ftpdownload

提问by jumbojs

What is the best way to download all files in a remote directory using C# and FTP and save them to a local directory?

使用 C# 和 FTP 下载远程目录中的所有文件并将它们保存到本地目录的最佳方法是什么?

Thanks.

谢谢。

回答by Martin Vobr

downloading all files in a specific folder seems to be an easy task. However, there are some issues which has to be solved. To name a few:

下载特定文件夹中的所有文件似乎是一项简单的任务。然而,有一些问题需要解决。仅举几例:

  • How to get list of files (System.Net.FtpWebRequest gives you unparsed list and directory list format is not standardized in any RFC)
  • What if remote directory has both files and subdirectories. Do we have to dive into the subdirs and download it's content?
  • What if some of the remote files already exist on the local computer? Should they be overwritten? Skipped? Should we overwrite older files only?
  • What if the local file is not writable? Should the whole transfer fail? Should we skip the file and continue to the next?
  • How to handle files on a remote disk which are unreadable because we don't have sufficient access rights?
  • How are the symlinks, hard linksand junction pointshandled? Links can easily be used to create an infinite recursive directory tree structure. Consider folder A with subfolder B which in fact is not the real folder but the *nix hard link pointing back to folder A. The naive approach will end in an application which never ends (at least if nobody manage to pull the plug).
  • 如何获取文件列表(System.Net.FtpWebRequest 为您提供未解析的列表和目录列表格式在任何 RFC 中都没有标准化)
  • 如果远程目录有文件和子目录怎么办。我们是否必须深入到子目录并下载它的内容?
  • 如果本地计算机上已经存在某些远程文件怎么办?他们应该被覆盖吗?跳过?我们应该只覆盖旧文件吗?
  • 如果本地文件不可写怎么办?整个传输应该失败吗?我们是否应该跳过该文件并继续下一个?
  • 如何处理远程磁盘上由于我们没有足够的访问权限而无法读取的文件?
  • 如何在符号链接硬链接结点进行处理?链接可以很容易地用于创建无限递归的目录树结构。考虑带有子文件夹 B 的文件夹 A,它实际上不是真正的文件夹,而是指向文件夹 A 的 *nix 硬链接。天真的方法将以永不结束的应用程序结束(至少如果没有人设法拔掉插头)。

Decent third party FTP component should have a method for handling those issues. Following code uses our Rebex FTP for .NET.

体面的第三方 FTP 组件应该有处理这些问题的方法。以下代码使用我们的Rebex FTP for .NET

using (Ftp client = new Ftp())
        {
            // connect and login to the FTP site
            client.Connect("mirror.aarnet.edu.au");
            client.Login("anonymous", "my@password");

            // download all files
            client.GetFiles(
                "/pub/fedora/linux/development/i386/os/EFI/*",
                "c:\temp\download",
                FtpBatchTransferOptions.Recursive,
                FtpActionOnExistingFiles.OverwriteAll
            );

            client.Disconnect();
        }

The code is taken from my blogpostavailable at blog.rebex.net. The blogpost also references a sample which shows how ask the user how to handle each problem (e.g. Overwrite/Overwrite older/Skip/Skip all).

该代码是从我拍摄的博文在blog.rebex.net可用。该博文还引用了一个示例,该示例展示了如何询问用户如何处理每个问题(例如覆盖/覆盖旧的/跳过/全部跳过)。

回答by Jérémie Bertrand

You can use FTPClient from laedit.net. It's under Apache license and easy to use.

您可以使用laedit.net 中的 FTPClient。它在 Apache 许可下,易于使用。

It use FtpWebRequest:

它使用FtpWebRequest

  • first you need to use WebRequestMethods.Ftp.ListDirectoryDetailsto get the detail of all the list of the folder
  • for each files you need to use WebRequestMethods.Ftp.DownloadFileto download it to a local folder
  • 首先您需要使用WebRequestMethods.Ftp.ListDirectoryDetails来获取文件夹的所有列表的详细信息
  • 对于您需要用于将WebRequestMethods.Ftp.DownloadFile其下载到本地文件夹的每个文件

回答by STW

You could use System.Net.WebClient.DownloadFile(), which supports FTP. MSDN Details here

您可以使用System.Net.WebClient.DownloadFile(),它支持 FTP。 此处为 MSDN 详细信息

回答by Handprint

Using C# FtpWebRequest and FtpWebReponse, you can use the following recursion (make sure the folder strings terminate in '\'):

使用 C# FtpWebRequest 和 FtpWebReponse,您可以使用以下递归(确保文件夹字符串以“\”结尾):

    public void GetAllDirectoriesAndFiles(string getFolder, string putFolder)
    {
        List<string> dirIitems = DirectoryListing(getFolder);
        foreach (var item in dirIitems)
        {
            if ( item.Contains('.')  )
            {
                GetFile(getFolder + item, putFolder + item);
            }
            else
            {
                var subDirPut = new DirectoryInfo(putFolder + "\" + item);
                subDirPut.Create();
                GetAllDirectoriesAndFiles(getFolder + item + "\", subDirPut.FullName + "\");
            }
        }
    }

The "item.Contains('.')" is a bit primitive, but has worked for my purposes. Post a comment if you need an example of the methods:

“item.Contains('.')”有点原始,但对我的目的有用。如果您需要这些方法的示例,请发表评论:

GetFile(string getFileAndPath, string putFileAndPath)

or

或者

DirectoryListing(getFolder)

回答by Martin Prikryl

For FTP protocol you can use FtpWebRequestclassfrom .NET framework. Though it does not have any explicit support for recursive file operations (including downloads). You have to implement the recursion yourself:

对于 FTP 协议,您可以使用.NET 框架中的FtpWebRequest。尽管它对递归文件操作(包括下载)没有任何明确的支持。您必须自己实现递归:

  • List the remote directory
  • Iterate the entries, downloading files and recursing into subdirectories (listing them again, etc.)
  • 列出远程目录
  • 迭代条目,下载文件并递归到子目录(再次列出它们等)

Tricky part is to identify files from subdirectories. There's no way to do that in a portable way with the FtpWebRequest. The FtpWebRequestunfortunately does not support the MLSDcommand, which is the only portable way to retrieve directory listing with file attributes in FTP protocol. See also Checking if object on FTP server is file or directory.

棘手的部分是从子目录中识别文件。使用FtpWebRequest. 该FtpWebRequest遗憾的是不支持的MLSD命令,这是检索目录与FTP协议的文件属性上市的唯一可移植的方法。另请参阅检查 FTP 服务器上的对象是文件还是目录

Your options are:

您的选择是:

  • Do an operation on a file name that is certain to fail for file and succeeds for directories (or vice versa). I.e. you can try to download the "name". If that succeeds, it's a file, if that fails, it's a directory. But that can become a performance problem, when you have a large number of entries.
  • You may be lucky and in your specific case, you can tell a file from a directory by a file name (i.e. all your files have an extension, while subdirectories do not)
  • You use a long directory listing (LISTcommand = ListDirectoryDetailsmethod) and try to parse a server-specific listing. Many FTP servers use *nix-style listing, where you identify a directory by the dat the very beginning of the entry. But many servers use a different format. The following example uses this approach (assuming the *nix format)
  • 对文件名进行操作,对于文件肯定会失败而对于目录会成功(反之亦然)。即您可以尝试下载“名称”。如果成功,它是一个文件,如果失败,它是一个目录。但是,当您有大量条目时,这可能会成为性能问题。
  • 您可能很幸运,在您的特定情况下,您可以通过文件名区分目录中的文件(即所有文件都有扩展名,而子目录没有)
  • 您使用长目录列表(LIST命令 =ListDirectoryDetails方法)并尝试解析特定于服务器的列表。许多 FTP 服务器使用 *nix 样式的列表,您可以通过d条目开头的 来标识目录。但是许多服务器使用不同的格式。以下示例使用此方法(假设为 *nix 格式)
void DownloadFtpDirectory(string url, NetworkCredential credentials, string localPath)
{
    FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(url);
    listRequest.UsePassive = true;
    listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
    listRequest.Credentials = credentials;

    List<string> lines = new List<string>();

    using (WebResponse listResponse = listRequest.GetResponse())
    using (Stream listStream = listResponse.GetResponseStream())
    using (StreamReader listReader = new StreamReader(listStream))
    {
        while (!listReader.EndOfStream)
        {
            lines.Add(listReader.ReadLine());
        }
    }

    foreach (string line in lines)
    {
        string[] tokens =
            line.Split(new[] { ' ' }, 9, StringSplitOptions.RemoveEmptyEntries);
        string name = tokens[8];
        string permissions = tokens[0];

        string localFilePath = Path.Combine(localPath, name);
        string fileUrl = url + name;

        if (permissions[0] == 'd')
        {
            Directory.CreateDirectory(localFilePath);
            DownloadFtpDirectory(fileUrl + "/", credentials, localFilePath);
        }
        else
        {
            FtpWebRequest downloadRequest = (FtpWebRequest)WebRequest.Create(fileUrl);
            downloadRequest.UsePassive = true;
            downloadRequest.UseBinary = true;
            downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            downloadRequest.Credentials = credentials;

            using (Stream ftpStream = downloadRequest.GetResponse().GetResponseStream())
            using (Stream fileStream = File.Create(localFilePath))
            {
                ftpStream.CopyTo(fileStream);
            }
        }
    }
}

The urlmust be like:

url一定很喜欢:

  • ftp://example.com/or
  • ftp://example.com/path/
  • ftp://example.com/或者
  • ftp://example.com/path/


Or use 3rd party library that supports recursive downloads.

或者使用支持递归下载的 3rd 方库。

For example with WinSCP .NET assemblyyou can download whole directory with a single call to Session.GetFiles:

例如,使用WinSCP .NET 程序集,您可以通过一次调用下载整个目录Session.GetFiles

// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = "example.com",
    UserName = "user",
    Password = "mypassword",
};

using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    // Download files
    session.GetFiles("/home/user/*", @"d:\download\").Check();
} 

Internally, WinSCP uses the MLSDcommand, if supported by the server. If not, it uses the LISTcommand and supports dozens of different listing formats.

MLSD如果服务器支持,WinSCP 在内部使用该命令。如果没有,它会使用该LIST命令并支持数十种不同的列表格式。

(I'm the author of WinSCP)

(我是 WinSCP 的作者)