【问题标题】:How could I add nodes of a treeview as folders from a remote directory with WinSCP and C#如何使用 WinSCP 和 C# 将树视图的节点添加为远程目录中的文件夹
【发布时间】:2016-08-05 05:47:46
【问题描述】:

我正在尝试创建一个树视图以使用 FTP/SFTP 连接在远程服务器中搜索目录,我正在尝试使用从主目录开始的所有可用目录开始填充树视图比如下面的例子:

Home---->SubFolder
    |
    |---->Another Folder
    |
    |---->MyOtherFolder

然后,当用户开始单击每个文件夹时,它开始从树视图中显示其子目录,如下例所示(单击另一个文件夹):

Home ---->SubFolder
     |
     |---->Another Folder -------> MyFolder1
     |                  | -------> MyFolder2
     |
     |---->MyOtherFolder 

我正在尝试获取这些文件夹,但它引发了异常,而且它正在收集文件,而不是文件夹......

这是我的代码......

private void FillTree()
{
   SessionOptions SessionOptions = new SessionOptions();
   Session MySession = new Session();

   SessionOptions.HostName = InterfaceValues[0];
   SessionOptions.UserName = InterfaceValues[2];
   SessionOptions.Password = InterfaceValues[3];
   SessionOptions.PortNumber = Convert.ToInt32(InterfaceValues[1]);

   if (string.Compare(InterfaceValues[9], "FTP", true) == 0)
       SessionOptions.Protocol = WinSCP.Protocol.Ftp;
   else if (string.Compare(InterfaceValues[9], "SFTP", true) == 0)
   {
        SessionOptions.Protocol = WinSCP.Protocol.Sftp;
        SessionOptions.SshPrivateKeyPath = InterfaceValues[12];
        SessionOptions.SshHostKeyFingerprint = InterfaceValues[10];
   }

   try
   {
       MySession.Open(SessionOptions);

       foreach (RemoteFileInfo info in MySession.EnumerateRemoteFiles("/", "*",  EnumerationOptions.AllDirectories))
       {
          if (info.IsDirectory)
             tvRemoteDirectory.Nodes.Add(info.Name);
       }

   MySession.Close();
}
catch (Exception ex)
{
     MySession.Close();
     MessageBox.Show("Not possible to connect to " + InterfaceValues[0] + "\nError Message: " + ex.Message);
      this.Close();
}

我得到的例外是:

{WinSCP.SessionRemoteException: Error listing directory '/jpm_icl'. ---> WinSCP.SessionRemoteException: Permission denied. Error code: 3 Error message from server: Permission Denied!

知道此时我能做什么吗?

【问题讨论】:

  • 要么获取权限,要么忽略错误。
  • 我试过这个:RemoteDirectoryInfo RemoteDirectory = MySession.ListDirectory("/"); foreach (RemoteFileInfo fileinfo in RemoteDirectory.Files) { //tvRemoteDirectory.Nodes.Add(fileinfo.Name); } 但它正在检索“。”和“..”和“jpm_icl”,但我不知道“jpm_icl”怎么样,我看不到那个文件夹
  • 什么时候得到异常?在EnumerateRemoteFiles 循环中?哪里看不到jpm_icl 文件夹?
  • 嗨@MartinPrikryl,是的,在循环中,第三次抛出异常......我的意思是,远程服务器中不存在jmp_icl,我不确定是否是一个隐藏文件夹。
  • 我已经在windows server中检查了文件夹和“显示隐藏文件和文件夹”选项并打开,不知道jmp_icl是什么

标签: c# .net treeview directory-structure winscp


【解决方案1】:

我做的是这样的:

ListDirectory 函数来检索所有目录,因为我不想要目录“。”和 ”。”我必须排除它。

RemoteDirectoryInfo RemoteDirectory;

     if (RemoteDirectoryPath != "Home")
          RemoteDirectory  = MySession.ListDirectory(RemoteDirectoryPath);
     else
          RemoteDirectory = MySession.ListDirectory("/");
     if (tvRemoteDirectory.SelectedNode.Nodes.Count > 0) tvRemoteDirectory.SelectedNode.Nodes.Clear();

     foreach (RemoteFileInfo fileinfo in RemoteDirectory.Files)
     {                    
        if (fileinfo.IsDirectory)
        {
           if (fileinfo.Name != "." &&
               fileinfo.Name != "..")
               {                                                    
                   TreeNode ChildNode = new TreeNode();
                   ChildNode.Text = fileinfo.Name;
                   ChildNode.ImageIndex = 0;                                                     
                   tvRemoteDirectory.SelectedNode.Nodes.Add(ChildNode);
                   tvRemoteDirectory.ExpandAll();
               }          
         }                    
     }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 2010-11-26
    • 2013-03-25
    • 2012-04-15
    • 2014-04-30
    • 2014-06-05
    相关资源
    最近更新 更多