使用FTP和C#下载所有文件 [英] Downloading all files using FTP and C#

查看:432
本文介绍了使用FTP和C#下载所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



谢谢。

解决方案

下载特定文件夹中的所有文件似乎是一件容易的任务。但是,有一些问题需要解决。举一个例子:




  • 如何获取文件列表(System.Net.FtpWebRequest为您提供未解析的列表和目录列表格式不标准化在任何RFC中)

  • 如果远程目录具有文件和子目录,该怎么办我们必须潜入这个子公司并下载它的内容吗?

  • 如果本地计算机上已存在一些远程文件,该怎么办?应该覆盖吗?跳过?我们应该只覆盖旧文件吗?

  • 如果本地文件不可写,该怎么办?整个转移是否会失败?我们应该跳过该文件并继续下一步吗?

  • 如何处理由于我们没有足够的访问权限而无法读取的远程磁盘上的文件?

  • 符号链接硬链接交汇点处理?链接可以轻松地用于创建无限递归目录树结构。考虑文件夹A与子文件夹B实际上不是真正的文件夹,但* nix硬链接指向文件夹A.幼稚的方法将结束于永远不会结束的应用程序(至少如果没有人设法拔出插件)。



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

  using(Ftp client = new Ftp())
{
//连接并登录到FTP站点
client.Connect(mirror.aarnet.edu.au);
client.Login(anonymous,我的@密码);

//下载所有文件
client.GetFiles(
/ pub / fedora / linux / development / i386 / os / EFI / *,
c :\\\\\,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

client.Disconnect();
}

代码取自我的 blogpost 可在blog.rebex.net。博客还引用了一个示例,其中显示了如何询问用户如何处理每个问题(例如覆盖/覆盖旧/跳过/全部跳过)。


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

Thanks.

解决方案

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 links and junction points handled? 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).

Decent third party FTP component should have a method for handling those issues. Following code uses our 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 blogpost available 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).

这篇关于使用FTP和C#下载所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆