简体   繁体   中英

How could I add nodes of a treeview as folders from a remote directory with WinSCP and C#

I'm trying to create a tree view to search for a directories in a remote server using FTP/SFTP connections, What I'm trying to do is start filling the tree view with all the available directories starting with the home directory such as the following example:

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

Then when the user start clicking in each folder it start to display their subdirectories from the tree view as the follwoing example (clicking in Another Folder):

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

I'm trying to get those folders but it's throwing an exception, also it is gathering files, not folders....

this is the code that I have....

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();
}

The exception that I'm getting is:

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

Any idea what could I do at this point?

What I did was this:

ListDirectory function to retrieve all the directories, as I don't want the directory "." and "." I have to exclude it.

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();
               }          
         }                    
     }

Use RadFileExplorer - Telerik ASP.NET FileExplorer

And this feature will complete your requirements FileExplorer - Show all items in the TreeView

All other Key Features are given below you would like it

From Telerik you can get this feature for any platform.

  • A single control, integrated in Telerik.Web.UI - ready to drag and drop on the page

  • Directory load on demand loading using ASP.NET 2.0 AJAX Callback mechanism

  • Clientside and server events for file operations

  • Uses the FileBrowserContentProvider abstraction of RadEditor for hooking to any underlying file system, such as OS, database, MOSS SharePoint, MCMS

  • Sorting of files and folders

  • Context menus

  • Ability to delete and rename files and folders

  • Ability to create new folders

  • Ability to enable paging for the grid if the folders contain a large number of items (eg more than 200)

  • Ability to switch quickly between Grid and Thumbnail views using the new toolbar buttons

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM