【问题标题】:Use "DirectoryInfo" with FTP server将“DirectoryInfo”与 FTP 服务器一起使用
【发布时间】:2015-05-16 15:29:27
【问题描述】:

我会使用这个指令:

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("ftp://192.168.47.1/DocXML");

但我不能。

如何将("ftp://192.168.47.1/DocXML");new System.IO.DirectoryInfo(""); 一起使用?

这是代码

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"\\192.168.47.1\DocXML");`

IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);

【问题讨论】:

    标签: c# ftp ftp-client directoryinfo system.io.directory


    【解决方案1】:

    恐怕你不能。

    试试这个:

    FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://192.168.47.1/DocXML");
    req.Credentials = new NetworkCredential("foo", "foo@foo.com");
    req.Method = WebRequestMethods.Ftp.ListDirectory;
    FtpWebResponse res = (FtpWebResponse)req.GetResponse();
    using (StreamReader streamReader = new StreamReader(res.GetResponseStream()))
    {
    ...
    }
    

    【讨论】:

    • 以及如何从 FTP 服务器下载创建日期最早的文件?
    • 解析流,它应该包含整个文件列表。然后,使用 FtpWebRequest/FtpWebResponse,下载最旧的。参考这个:link
    • 但我想知道 FTP 服务器地毯上最旧的文件,而 FtpWebRequest/FtpWebResponse 对我没有帮助。
    • 抱歉,请使用req.Method = WebRequestMethods.Ftp.ListDirectoryDetails; 并解析响应以选择最旧的,然后下载。
    【解决方案2】:

    如果您需要有关 FTP 目录中文件的结构化信息,则必须使用第 3 方库。 .NET 框架不提供此类功能。

    特别是因为它不支持MLSD FTP 命令,所以检索远程文件及其属性的机器可读列表的唯一可靠方法是什么。


    有许多 3rd 方库允许这样做。

    例如WinSCP .NET assembly:

    // Setup session options
    SessionOptions sessionOptions = new SessionOptions
    {
        Protocol = Protocol.Ftp,
        HostName = "example.com",
        UserName = "username",
        Password = "password",
    };
    
    using (Session session = new Session())
    {
        // Connect
        session.Open(sessionOptions);
    
        // Get list of files in the directory
        string remotePath = "/remote/path/";
        RemoteDirectoryInfo directoryInfo = session.ListDirectory(remotePath);
    
        foreach (RemoteFileInfo fileInfo in directoryInfo.Files)
        {
            Console.WriteLine("{0} with size {1}, permissions {2} and last modification at {3}",
                fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions,
                fileInfo.LastWriteTime);
        }
    }
    

    参考资料:
    https://winscp.net/eng/docs/library_session_listdirectory
    https://winscp.net/eng/docs/library_remotefileinfo

    从您的commentother question,您似乎实际上需要检索FTP 目录中最旧的文件。为此,请参阅:

    两者都是最新的,而不是最旧的文件。只需将 C# 代码中的 .OrderByDescending 替换为 .Order 即可获取最旧的文件。

    (我是 WinSCP 的作者)

    【讨论】:

      【解决方案3】:

      不能以这种方式工作。 我建议使用 SFTP 而不是 FTP。为此,我正在使用第 3 方库“SharpSSH”。 以下示例似乎有效:

      using System.IO;
      using Tamir.SharpSsh;
      using Tamir.SharpSsh.jsch;
      
      string ip = "DestinationIp";
      string user = "JohnDoe";
      string password = "YourPassword";
      Sftp sftp = new Tamir.SharpSsh.Sftp(ip, user, password);
      sftp.Connect();
      
      FileInfo yourFileInfo = new FileInfo("path");
      

      还可以添加主键 sftp.AddIdentityFile();

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多