【问题标题】:How to distinguish a directory in SFTP listing retrieved using SharpSSH?如何区分使用 SharpSSH 检索的 SFTP 列表中的目录?
【发布时间】:2015-03-09 07:37:37
【问题描述】:

我需要通过 SharpSsh 将文件从服务器递归下载到 SFTP。但我不明白如何确定文件名或目录。 现在我这样做了

 static void recDir(string remotePath, string localPath)
 {
     Sftp _c = this.sftp;
     ArrayList FileList = _c.GetFileList(remotePath);
     FileList.Remove(".");
     FileList.Remove("..");
     for (int i = 0; i < FileList.Count; i++)
     {
         try
         {
             _c.Get(remotePath + "/" + FileList[i], localPath + "/" + FileList[i]);

             Console.WriteLine("File: " + remotePath + "/" + FileList[i]);
         }
         catch (Exception e)
         {
             Console.WriteLine("Dir: " + remotePath + "/" + FileList[i]);
                System.IO.Directory.CreateDirectory(localPath + "/" + FileList[i]);
                recDir(remotePath + "/" + FileList[i], localPath + "/" + FileList[i]);
         }
     }
 }

有效,但似乎不正确。

【问题讨论】:

  • 您的代码似乎发生了任何异常,然后您尝试递归但不清楚要递归的位置..
  • 不知道这是什么意思:It works, but it seems not correct.
  • @Dan-o 我很确定这意味着 OP 正在尝试编写健壮的代码,而不是“恰好工作”的代码。对常规程序流使用异常是很臭的。

标签: c# sftp sharpssh


【解决方案1】:

SharpSSH API 不允许这样做。但是您可以将其编码为 SharpSSH 是开源的。请注意,SharpSSH 是一个糟糕的选择。多年没有维护。


查看我对另一个类似 SharpSSH 问题的回答:

或使用其他 SFTP 库:

  • SSH.NET 有方法 SftpClient.ListDirectory 返回 IEnumerable&lt;SftpFile&gt;SftpFile 具有 .IsDirectory 属性
  • WinSCP .NET assembly 具有方法 Session.ListDirectory 返回(通过 RemoteDirectoryInfo.Files)具有属性 RemoteFileInfo 的集合 .IsDirectory
    (我是 WinSCP .NET 程序集的作者)

【讨论】:

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