tree view of server file

0 votes
asked Oct 22, 2010 by Neha (140 points)
edited Mar 25, 2013

My server have drives and folders hierarchy.i need to see all the files,folders and that subfolders on the server like treeview formate for downloading.I am able to connect to server.

please help me.

Applies to: Rebex FTP/SSL

1 Answer

+1 vote
answered Oct 22, 2010 by Lukas Pokorny (128,250 points)
edited Mar 25, 2013

You can easily construct a tree made of TreeNode objects yourself by calling Ftp object's GetList method repeatedly. Just use a method like this one:

    public static TreeNode GetListTree(Ftp ftp, string folder)
    {
        // create a tree node
        TreeNode node = new TreeNode(folder);

        // save current folder
        string originalFolder = ftp.GetCurrentDirectory();

        // change current directory to the next folder
        ftp.ChangeDirectory(folder);

        // get list of files in the current folder
        FtpList list = ftp.GetList();

        // iterate through the list of files
        foreach (FtpItem item in list)
        {
            TreeNode subNode;
            if (item.IsDirectory)
            {
                // if an item is a directory, construct
                // a tree for it recursively
                subNode = GetListTree(ftp, item.Name);
            }
            else
            {
                // if an item is not a directory,
                // just add it to the tree
                subNode = new TreeNode(item.Name);
            }

            // add the subnode to current node's nodes
            node.Nodes.Add(subNode);
        }

        // go back to the original folder
        ftp.ChangeDirectory(originalFolder);

        return node;
    }

Call this on the folder you wish to appear in the TreeView and add the result to TreeView's Nodes collection.

commented Mar 23, 2013 by Behzad (110 points)
edited Mar 23, 2013

If I want to add a key for each node that is a directory node, how can I do that?

commented Mar 25, 2013 by Lukas Pokorny (128,250 points)
edited Mar 25, 2013

I'm not quite sure what you mean by 'key' in this context, and where would you like to add it. Could you please explain?

...