Ah man , ive been adjusting the code so much ...

I am trying to make a treeview that display's all the directories and files on theserver (SO FAR SO GOOD) ...

Here's my code to retrieve Directories and files

public string[] GetFileList()
        {
            string[] downloadFiles;
            StringBuilder result = new StringBuilder();
            FtpWebRequest reqFTP;
            try
            {
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + txtftpServerName.Text + "/"));
                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();
                
                return result.ToString().Split('\n');
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                downloadFiles = null;
                return downloadFiles;
            }
        }

public string[] GetChildFileList()
        {
            string[] downloadFiles;
            StringBuilder result = new StringBuilder();
            FtpWebRequest reqFTP;
            try
            {
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + txtftpServerName.Text + "/" + treeView1.SelectedNode.Text + "/"));
                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();

                return result.ToString().Split('\n');
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                downloadFiles = null;
                return downloadFiles;
            }
        }

This works fine indevidually , but when i try to combine the two:

private void btnDoIt_Click(object sender, EventArgs e)
        {
            string[] Parent_Name = GetFileList();
            string[] Child_Name = GetChildFileList();

            treeView1.Nodes.Clear();
            if (Parent_Name != null)
            {
                /*          Parent Node             */
                foreach (string filename in Parent_Name)
                {
                    TreeNode ParentNode = new TreeNode(filename);
                    treeView1.Nodes.Add(ParentNode);

                    if (Parent_Name != null)
                    {
                        foreach (string filename2 in Child_Name)
                        {
                            TreeNode ChildNode = new TreeNode(filename2);
                            ParentNode.Nodes.Add(ChildNode);
                        }
                    }
                }
            }
        }

i get an error stating that "Object reference not set to an instance of an object." (LENS ON [foreach (string filename2 in Child_Name)] )

Can anyone maybe give me a clue ?? i dont even know why its complaining because it aught to work...

Happy coding

Recommended Answers

All 8 Replies

It works with me! but try to debug, that's the one who you can fully trust in.

Its almost certainly correct:

as you go to use Child_name, but you checked parent wasnt null, so child could be

It also works proper with me, but have some thing, that you should look at:
1. You check Parent_Name 2 times. And second one is inside the first one. It looks like question: "Are you really sure that (Parent_Name != null)" ;P

treeView1.Nodes.Clear();
            if (Parent_Name != null)
            {

and

if (Parent_Name != null)
                    {
                        foreach (string filename2 in Child_Name)

2. I suppose you should do that:

treeView1.Nodes.Add(ParentNode);

after you fill with data the ChildNode of this ParentNode.


Good luck :)

Which is what I said in a lot less of a post.

commented: :( sowwy ... some time my carelessness will kill me O.o +1

Oh yeah :( sorry. First part is your's. Wasn't about to do things like that O.O ... it seems that I need rest a bit ;P

Welll well well, i feel like a retard now hahahahahahahaha!!!

Although , thats not the problem...

The problem lies here

string[] Child_Name = GetChildFileList();

for some reason (i think) the Uri does not change because you have to SELECT a node first...

So i did this

private void btnDoIt_Click(object sender, EventArgs e)
        {
            string[] Parent_Name = GetFileList();
            

            treeView1.Nodes.Clear();
            if (Parent_Name != null)
            {

                /*          Parent Node             */
                foreach (string filename in Parent_Name)
                {
                    TreeNode ParentNode = new TreeNode(filename);
                    
                    // changed the GetChildFileList uri to 'reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + txtftpServerName.Text + "/" + txtShowDirectory.Text + "/"));'

                    [B][U]txtShowDirectory.Text = filename;[/U][/B]

                    string[] Child_Name = GetChildFileList();

                    if (Child_Name != null)
                    {
                        foreach (string filename2 in Child_Name)
                        {
                            TreeNode ChildNode = new TreeNode(filename2);
                            ParentNode.Nodes.Add(ChildNode);
                        }
                    }
                    treeView1.Nodes.Add(ParentNode);
                }
            }
        }

WORKS LIKE A BOMB!!!!!!!!

cept i still get an error on my try -> catch saying

"The remote server returned an error: (550) File unavailable. (eg. File not found, unavailable)."

Obviously there is still some kind of error

Look i know the problemo is this

string[] Child_Name = GetChildFileList();
if (Child_Name != null)
{
foreach (string filename2 in Child_Name)
{
TreeNode ChildNode = new TreeNode(filename2);
ParentNode.Nodes.Add(ChildNode);
}
}

but i cant really bypass that one or can I ???

Well if Child_name is null, then look at your function

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.