栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

C#通过FTP下载所有文件和子目录

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#通过FTP下载所有文件和子目录

FtpWebRequest
没有递归文件操作(包括下载)任何明确的支持。您必须自己实现递归:

  • 列出远程目录
  • 迭代条目,下载文件并递归到子目录(再次列出它们,等等)

棘手的部分是从子目录中识别文件。使用。不能以可移植的方式执行此操作

FtpWebRequest
。该
FtpWebRequest
遗憾的是不支持的
MLSD
命令,这是检索目录与FTP协议的文件属性上市的唯一可移植的方法。另请参阅检查FTP服务器上的对象是文件还是目录。

您的选择是:

  • 对一定要对文件失败但对目录成功的文件名执行操作(反之亦然)。即,您可以尝试下载“名称”。如果成功,则为文件;如果失败,则为目录。
  • 您可能很幸运,在您的特定情况下,您可以通过文件名告诉目录中的文件(即,所有文件都有扩展名,而子目录则没有)
  • 您使用长目录列表(

    LIST
    command =
    ListDirectoryDetails
    method)并尝试解析服务器特定的列表。许多FTP服务器使用 nix样式的列表,您可以
    d
    在条目的开始处通过来标识目录。但是许多服务器使用不同的格式。下面的示例使用这种方法(假设
    nix格式)

    void DownloadFtpDirectory(string url, NetworkCredential credentials, string localPath)
    {
    FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(url);
    listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
    listRequest.Credentials = credentials;

    List<string> lines = new List<string>();using (FtpWebResponse listResponse = (FtpWebResponse)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')    {        if (!Directory.Exists(localFilePath))        { Directory.CreateDirectory(localFilePath);        }        DownloadFtpDirectory(fileUrl + "/", credentials, localFilePath);    }    else    {        FtpWebRequest downloadRequest = (FtpWebRequest)WebRequest.Create(fileUrl);        downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile;        downloadRequest.Credentials = credentials;        using (FtpWebResponse downloadResponse =       (FtpWebResponse)downloadRequest.GetResponse())        using (Stream sourceStream = downloadResponse.GetResponseStream())        using (Stream targetStream = File.Create(localFilePath))        { byte[] buffer = new byte[10240]; int read; while ((read = sourceStream.Read(buffer, 0, buffer.Length)) > 0) {     targetStream.Write(buffer, 0, read); }        }    }}

    }

使用如下功能:

NetworkCredential credentials = new NetworkCredential("user", "mypassword");string url = "ftp://ftp.example.com/directory/to/download/";DownloadFtpDirectory(url, credentials, @"C:targetdirectory");

如果要避免解析特定于服务器的目录列表格式的麻烦,请使用支持该

MLSD
命令和/或解析各种
LIST
列表格式的第三方库。和递归下载。

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

Session.GetFiles

// Setup session optionsSessionOptions sessionOptions = new SessionOptions{    Protocol = Protocol.Ftp,    HostName = "ftp.example.com",    UserName = "user",    Password = "mypassword",};using (Session session = new Session()){    // Connect    session.Open(sessionOptions);    // Download files    session.GetFiles("/directory/to/download/*", @"C:targetdirectory*").Check();}

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

默认情况下,该

Session.GetFiles
方法是递归的。

(我是WinSCP的作者)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/367441.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号