I am making a recursive treeview that lists the directories on my server [or trying atleast]...

So far so good , it seems to work, cept it does not load all the files / directories, please consider my code :

void recurese(string root)
        {

            // New instance of the StringBuilder Class (resluts)
            StringBuilder result = new StringBuilder();

            // New instance of FtpWebRequest (reqFTP)
            FtpWebRequest reqFTP;

            // Setting root to ftp://ngaleafrica.co.za
            root = "ftp://" + txtFtpServerName.Text;
            try
            {
                // Ceating URI for reqFTP , Setting URI to ROOT
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(root));

                // Setting Binary to true
                reqFTP.UseBinary = true;

                // Setting credentials for reqFTP
                reqFTP.Credentials = new NetworkCredential(txtFtpUserName.Text, txtFtpPassword.Text);

                // Setting method of reqFTP to get a list of directories
                reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;

                // Creating FtpWebResponse, response gets the webrequest
                WebResponse response = reqFTP.GetResponse();

                // Creating new stream reader to read the response
                StreamReader reader = new StreamReader(response.GetResponseStream());

                // Creatinf string (line) that reads the streamreader lines
                string line = reader.ReadLine();

                // While the line is not null
                while (line != null)
                {
                    // StringBuilder adds line (the streamreader line)
                    result.Append(line);

                    // Add a line break to the line
                    result.Append("\n");

                    // telling line to read another line of the streamreader
                    line = reader.ReadLine();
                }

                // Removing last linebreak from Stringbuilder...
                result.Remove(result.ToString().LastIndexOf('\n'), 1);

                // Closing the StremReader
                reader.Close();

                // Closing FtpWebResponse
                response.Close();

                // Creating array RESULT
                // Splitting Streamreader string where linebreak occurs
                string[] Result =  result.ToString().Split('\n');

                // If Result is not null or void whatever
                if (Result != null)
                {
                    // Foreach string in the string array Result
                    foreach (string ParentDir in Result)
                    {
                        // Add Parent Node to hold values for strings (first level)
                        TreeNode ParentNode = new TreeNode(ParentDir);

                        if (ParentDir.Contains("."))
                        { }
                        else
                        {
                            // Adding ParentNode to treeview
                            treeView1.Nodes.Add(ParentNode);

                            // Showing the Directory ... not really important
                            txtShowDirectory.Text = root + "/" + ParentDir;

                            // Showing the Directory ... not really important
                            Console.WriteLine("Txt Show Dir : " + root + "/" + ParentDir);

                            // Show the parentnode text
                            Console.WriteLine("Adding Parent node : " + ParentNode.Text);

                            // Setting bool Isfile to false
                            Isfile = false;
                                
                            // Calling recurse child method
                            recureseChild(ParentNode, root + "/" + ParentDir);

                        }
                    }
                    // Refreshing the treeview
                    treeView1.Refresh();
                    }
                }
            catch (Exception ex)
            {
                Console.WriteLine("Error in 'recurse' : " + ex.Message);
            }
        }

        void recureseChild(TreeNode Node, string root)
        {
            if (Isfile == false)
            {
                // New instance of the StringBuilder Class (resluts)
                StringBuilder result = new StringBuilder();

                // New instance of FtpWebRequest (reqFTP)
                FtpWebRequest reqFTP;

                root = root;

                Console.WriteLine("Child Root : " + root);
                try
                {
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(root));
                    reqFTP.UseBinary = true;
                    reqFTP.Credentials = new NetworkCredential(txtFtpUserName.Text, txtFtpPassword.Text);
                    reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;

                    WebResponse response = reqFTP.GetResponse();

                    StreamReader reader = new StreamReader(response.GetResponseStream());

                    string line = reader.ReadLine();
                    while (line != null)
                    {
                        result.Append(line);
                        result.Append("\n");
                        line = reader.ReadLine();
                    }
                    result.Remove(result.ToString().LastIndexOf('\n'), 1);
                    reader.Close();
                    response.Close();

                    string[] Result = result.ToString().Split('\n');

                    if (Result != null)
                    {
                        foreach (string ChildDir in Result)
                        {
                            TreeNode ChildNode = new TreeNode(ChildDir);
                            Console.WriteLine("Start Childnode :" + ChildNode.Text);
                            Node.Nodes.Add(ChildNode);

                            recureseChild(ChildNode, root + "/" + ChildDir + "/");
                        }
                    }
                }

                catch (Exception ex)
                {
                    Console.WriteLine("Failure in Recurse Child : " + ex.Message);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            recurese(root);
        }

The problem lies here


foreach (string ChildDir in Result)
{
TreeNode ChildNode = new TreeNode(ChildDir);
Console.WriteLine("Start Childnode :" + ChildNode.Text);
Node.Nodes.Add(ChildNode);

recureseChild(ChildNode, root + "/" + ChildDir + "/");
}


I think its not loading the directories in the right way

I get this exception each in the debug

Failure in Recurse Child : The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

&

A first chance exception of type 'System.Net.WebException' occurred in System.dll

Please help ??

Any Ideas ...:icon_cool:

The path would be

tfp://servername + "/" + ChildDir + "/"

The path it cannot find is /dir1/dir2/dir3

i take it, there must be a problem loading the recurseChild within the recurseChild, can you check that out and maybe give me a tip, i am clueless, you know ive been on this one thing for 2 weeks now ...

No, not necessarily - what is the actual value of the path rather than a fake one of dir1/dir2/dir3

are you sure the value of dir3 is a directory for example

hahaha, ok i get what you're saying... let me put it this way

ftp://theserver.com/datasafe_backup/Database_Differential_Backup.bak.zip/

<----- We know this one is stopping there----->

Failure in Recurse Child : The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
A first chance exception of type 'System.Net.WebException' occurred in System.dll

Start Childnode :public_html/es
Child Root : ftp://theserver.com/public_html/public_html/es/

--->This one must continue<---

A first chance exception of type 'System.Net.WebException' occurred in System.dll
Failure in Recurse Child : The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

Are you sure you are getting the new directory child's at every pass ?

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(root));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(txtFtpUserName.Text, txtFtpPassword.Text);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;

because it's seems the public_html/public_html/es can be the problem

you have a directory with no child's it gives the file exception you said that's ok .

but when it find's a child with a child it double the old path so now it's child/child/newChild and because the path is not found it gives an error.

I hope you understand what I'm asking.

I understand what you are saying , but i dont nderstand what you are asking...

I will quickly try and fix that , please try and help me ?? i havent slept in a few days now ... hehehe

miculnegru = GENIUS

But

Start Childnode :public_html/es
Child Root : ftp://theserver.com/public_html/public_html/es/

--->This one must continue<---

A first chance exception of type 'System.Net.WebException' occurred in System.dll
Failure in Recurse Child : The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

ISNT valid - your code is makingthe path wrong, public_html/public_html/es isnt the path.. The FTP response is correct.

The other thing is not that the directory has necessarily no children, but more that you''re trying to directory inside a file.

PS if you havent slept you will get more thick headed and more frustrated - sleep.

can it be this simple ? :

change this :

// Calling recurse child method
recureseChild(ParentNode, root + "/" + ParentDir);

in this :

// Calling recurse child method
recureseChild(ParentNode, root + "/" + ParentDir+"/");

Thanks LizR, i only see that now ... but now i have been trying to manipulate the recurseChild's Root, i cant really get it there, ... what to do , what to do .... give me a while, thanks for the advice people , i love you <3

The principal of recursion is actually simple. There are a plethora of examples of it all over.

YAAAAYYYYYY, you guys are awesome , it works now !!!! finally

one problem still though = it list all the directories, yet it lists no files

How can this be fixed ???

here's my current code :

void recurese(string root)
        {

            // New instance of the StringBuilder Class (resluts)
            StringBuilder result = new StringBuilder();

            // New instance of FtpWebRequest (reqFTP)
            FtpWebRequest reqFTP;

            // Setting root to server
            root = "ftp://" + txtFtpServerName.Text;
            try
            {
                // Ceating URI for reqFTP , Setting URI to ROOT
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(root));

                // Setting Binary to true
                reqFTP.UseBinary = true;

                // Setting credentials for reqFTP
                reqFTP.Credentials = new NetworkCredential(txtFtpUserName.Text, txtFtpPassword.Text);

                // Setting method of reqFTP to get a list of directories
                reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;

                // Creating FtpWebResponse, response gets the webrequest
                WebResponse response = reqFTP.GetResponse();

                // Creating new stream reader to read the response
                StreamReader reader = new StreamReader(response.GetResponseStream());

                // Creatinf string (line) that reads the streamreader lines
                string line = reader.ReadLine();

                // While the line is not null
                while (line != null)
                {
                    // StringBuilder adds line (the streamreader line)
                    result.Append(line);

                    // Add a line break to the line
                    result.Append("\n");

                    // telling line to read another line of the streamreader
                    line = reader.ReadLine();
                }

                // Removing last linebreak from Stringbuilder...
                result.Remove(result.ToString().LastIndexOf('\n'), 1);

                // Closing the StremReader
                reader.Close();

                // Closing FtpWebResponse
                response.Close();

                // Creating array RESULT
                // Splitting Streamreader string where linebreak occurs
                string[] Result =  result.ToString().Split('\n');

                // If Result is not null or void whatever
                if (Result != null)
                {
                    // Foreach string in the string array Result
                    foreach (string ParentDir in Result)
                    {
                        // Add Parent Node to hold values for strings (first level)
                        TreeNode ParentNode = new TreeNode(ParentDir);

                        if (ParentDir.Contains("."))
                        { }
                        else
                        {
                            // Adding ParentNode to treeview
                            treeView1.Nodes.Add(ParentNode);

                            // Showing the Directory ... not really important
                            Console.WriteLine("Txt Show Dir : " + root + "/" + ParentDir);

                            // Show the parentnode text
                            Console.WriteLine("Adding Parent node : " + ParentNode.Text);

                            // Setting bool Isfile to false
                            Isfile = false;
                                
                            // Calling recurse child method
                            recureseChild(ParentNode, root + "/" + ParentDir + "/");

                        }
                    }
                    // Refreshing the treeview
                    treeView1.Refresh();
                    }
                }
            catch (Exception ex)
            {
                Console.WriteLine("Error in 'recurse' : " + ex.Message);
            }
        }

        void recureseChild(TreeNode Node, string root)
        {
            if (Isfile == false)
            {
                // New instance of the StringBuilder Class (resluts)
                StringBuilder result = new StringBuilder();

                // New instance of FtpWebRequest (reqFTP)
                FtpWebRequest reqFTP;

                root = root;

                Console.WriteLine("Child Root : " + root);
                try
                {
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(root));
                    reqFTP.UseBinary = true;
                    reqFTP.Credentials = new NetworkCredential(txtFtpUserName.Text, txtFtpPassword.Text);
                    reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;

                    WebResponse response = reqFTP.GetResponse();

                    StreamReader reader = new StreamReader(response.GetResponseStream());

                    string line = reader.ReadLine();
                    while (line != null)
                    {
                        result.Append(line);
                        result.Append("\n");
                        line = reader.ReadLine();
                    }
                    result.Remove(result.ToString().LastIndexOf('\n'), 1);
                    reader.Close();
                    response.Close();

                    string[] Result = result.ToString().Split('\n');

                    if (Result != null)
                    {
                        foreach (string ChildDir in Result)
                        {
                            TreeNode ChildNode = new TreeNode(ChildDir);
                            if (ChildDir.Contains("."))
                            { }
                            else
                            {
                                Console.WriteLine("Start Childnode :" + ChildNode.Text);

                                Node.Nodes.Add(ChildNode);
                                // MessageBox.Show("Start Childnode :" + txtFtpServerName.Text + "/" + ChildNode.Text);
                                recureseChild(ChildNode, root + ChildDir + "/");
                            }
                        }
                    }
                }

                catch (Exception ex)
                {
                    Console.WriteLine("Failure in Recurse Child : " + ex.Message);
                }
            }
        }

because you have only told it how to handle directories if you can enter them, you havent handled directories that arent accessible, or are files - because you add them only if the child directory contains "."

You meant if it does NOT contain "."

Im doing this now :

foreach (string ChildDir in Result)
                        {
                            TreeNode ChildNode = new TreeNode(ChildDir);
                            if (ChildDir.Equals("."))
                            { }
                            else
                            {
                                Console.WriteLine("Start Childnode :" + ChildNode.Text);

                                Node.Nodes.Add(ChildNode);
                                // MessageBox.Show("Start Childnode :" + txtFtpServerName.Text + "/" + ChildNode.Text);
                                recureseChild(ChildNode, root + ChildDir + "/");
                                recureseChild(ChildNode, root + ChildDir);
                            }

coz if i go into the "." dir it will be an endless loop ...

unfortunately it takes like 20minutes to go through my server...but i will get there now hehe

So

Debug, you tell us what you missed, and whats happenign when it hits a file rather than a directory - or a directory it cant go into...

:)

This article has been dead for over six months. Start a new discussion instead.