7

Below is my code

DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Users\Shahul\Documents\Visual Studio 2010\Projects\TreeView\TreeView\bin\FileExplorer");

private void Form1_Load(object sender, EventArgs e)
{
    if (Directory.Exists("FileExplorer"))
    {
        try
        {
            DirectoryInfo[] directories = directoryInfo.GetDirectories();

            foreach (FileInfo file in directoryInfo.GetFiles())
            {
                if (file.Exists)
                {
                    TreeNode nodes = treeView.Nodes[0].Nodes.Add(file.Name);
                }
            }

            if (directories.Length > 0)
            {
                foreach (DirectoryInfo directory in directories)
                {
                    TreeNode node = treeView.Nodes[0].Nodes.Add(directory.Name);
                    node.ImageIndex = node.SelectedImageIndex = 0;
                    foreach (FileInfo file in directory.GetFiles())
                    {
                        if (file.Exists)
                        {
                            TreeNode nodes = treeView.Nodes[0].Nodes[node.Index].Nodes.Add(file.Name);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

When I run I just get a blank treeview form? Unable to figure out what is the error?

Btw this my first post in Stack Overflow.

John Willemse
  • 6,608
  • 7
  • 31
  • 45
shahul ha
  • 123
  • 1
  • 2
  • 12
  • I copied your code and it seems to be working on my end. (Besides getting warnings about certain folders being unreadable because of access rights) By the way, try to make this function into a recursive one for adding the nodes and whatnot. Might give you better clarity of what is happening and where your error might be. Also, you are setting a variable nodes with which you do nothing (TreeNode nodes gets assigned but is not used). – Ken de Jong May 01 '13 at 08:57
  • Debug and make sure that the tree does contain elements before the function returns. Maybe you're adding nodes to the wrong tree. Maybe the tree is cleared somewhere directly afterwards. Debug. – SimpleVar May 01 '13 at 09:01

4 Answers4

11

This should solve your problem, I tried on WinForm though:

public Form1()
    {
        InitializeComponent();

        DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Users\hikuma\Documents\IR");
        if (directoryInfo.Exists)
        {
            treeView1.AfterSelect += treeView1_AfterSelect;
            BuildTree(directoryInfo, treeView1.Nodes);
        }
    }

    private void BuildTree(DirectoryInfo directoryInfo, TreeNodeCollection addInMe)
    {
        TreeNode curNode = addInMe.Add(directoryInfo.Name);

        foreach (FileInfo file in directoryInfo.GetFiles())
        {
            curNode.Nodes.Add(file.FullName, file.Name);
        }
        foreach (DirectoryInfo subdir in directoryInfo.GetDirectories())
        {
            BuildTree(subdir, curNode.Nodes);
        }
    }

    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
        if(e.Node.Name.EndsWith("txt"))
        {
            this.richTextBox1.Clear();
            StreamReader reader = new StreamReader(e.Node.Name);
            this.richTextBox1.Text = reader.ReadToEnd();
            reader.Close();
        }
    }

It is a simple example of how you can open file in rich text box, it can be improved a lot :). You might want to mark as answer or vote up if it helped :) !!

Himanshu Kumar
  • 541
  • 5
  • 14
  • thanks a lot it solved the problem. Another quick help how do i display the contents into rich text box i.e lets say under my treeview folder structure i have some notepad files the moment i select any notepad or double click it should display the content into richtext box... – shahul ha May 01 '13 at 09:24
  • Thanks you for your help, I just pasted your code on my form it compiles without any problem. However when i click my text file displayed in my tree view strucure nothing is displayed in richtextbox. I do have some some contents in my text file – shahul ha May 01 '13 at 10:25
  • Add a rich text box on your form and it will display file content in your text box... – Himanshu Kumar May 01 '13 at 10:34
  • I have already added the box – shahul ha May 01 '13 at 10:35
  • What is the extension of your file, if you notice I have a if block checking if the file is a text file with (.txt) extension then only open it and display in text box. – Himanshu Kumar May 01 '13 at 10:36
  • yes its .txt extension – shahul ha May 01 '13 at 10:37
  • Just for your reference http://postimg.org/image/6ltcoosm5/ – shahul ha May 01 '13 at 10:40
  • @shahulha , Though this is **7 years old, for the love of code..** Change the code to check if it ends with **.txt , not txt** – Momoro Mar 29 '20 at 04:50
0

Try this: (note make sure your directoryInfo location contains some folders)

DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Users\Shahul\Documents\Visual Studio 2010\Projects\TreeView\TreeView\bin\FileExplorer");

private void Form1_Load(object sender, EventArgs e)
{
    if (directoryInfo.Exists)
    {
        try
        {
            treeView.Nodes.Add(directoryInfo.Name);
            DirectoryInfo[] directories = directoryInfo.GetDirectories();

            foreach (FileInfo file in directoryInfo.GetFiles())
            {
                if (file.Exists)
                {
                    TreeNode nodes = treeView.Nodes[0].Nodes.Add(file.Name);
                }
            }


            if (directories.Length > 0)
            {
                foreach (DirectoryInfo directory in directories)
                {
                    TreeNode node = treeView.Nodes[0].Nodes.Add(directory.Name);
                    node.ImageIndex = node.SelectedImageIndex = 0;
                    foreach (FileInfo file in directory.GetFiles())
                    {
                        if (file.Exists)
                        {
                            TreeNode nodes = treeView.Nodes[0].Nodes[node.Index].Nodes.Add(file.Name);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}
Hendrik T
  • 191
  • 7
0

DirectoryInfo.Exists("FileExplorer") will check for "C:\Users\Shahul\Documents\Visual Studio 2010\Projects\TreeView\TreeView\bin\debug\FileExplorer", not "C:\Users\Shahul\Documents\Visual Studio 2010\Projects\TreeView\TreeView\bin\FileExplorer", when you are running in debug mode.

teej
  • 135
  • 3
  • 11
0

Try the following:

private void Form1_Load(object sender, EventArgs e)
    {
        if (directoryInfo.Exists)
        {
            try
            {
                treeView.Nodes.Add(LoadDirectory(directoryInfo));                    
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

    private TreeNode LoadDirectory(DirectoryInfo di)
    {
        if (!di.Exists)
            return null;

        TreeNode output = new TreeNode(di.Name, 0, 0);

        foreach (var subDir in di.GetDirectories())
        {
            try
            {
                output.Nodes.Add(LoadDirectory(subDir));
            }
            catch (IOException ex)
            {
                //handle error
            }
            catch { }
        }

        foreach (var file in di.GetFiles())
        {
            if (file.Exists)
            {
                output.Nodes.Add(file.Name);
            }
        }

        return output;
    }
}

It's better to split out the directory parsing into a recursive method so that you can go all the way down the tree.

This WILL block the UI until it's completely loaded - but fixing that is beyond the scope of this answer...

:)

  • No probs! In future, make sure you let us know if you're working with winforms/WPF, as they handle things differently. – Chemass May 01 '13 at 10:37