Skip to content

How to get a TreeView of a specific network directory

Gerben

Asked Oct 11, 2007, 6:09 AM EDT3 answers
Share
LinkedInX

Hi, (beginner in programming c#)

How can I get a TreeView of a specific Network directory and its subdirectories?

For my "Solution" in Visual Studio I also have created a settings form to select a "Projects" directory: e.g. "P:\Project".
This "Projects" directory I want to show in another form with TreeView/ListView.
The textbox where this directory is set is also a application setting (text) called Project_Dir.

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    Here's a simple example to enumerate the file system:

    C#
    private void button1_Click(object sender, EventArgs e)
    {
        DirectoryInfo startFolder = new DirectoryInfo(@"P:\Project");
        TreeNode rootNode = treeView1.Nodes.Add(startFolder.Name);
    
        CreateFileSystemNodes(rootNode, startFolder);
    }
    
    private void CreateFileSystemNodes(TreeNode currentNode, DirectoryInfo currentFolder)
    {
        // add all sub-folders within the current folder
        foreach (DirectoryInfo subfolder in currentFolder.GetDirectories())
        {
            TreeNode subFolderNode = currentNode.Nodes.Add(subfolder.Name);
            CreateFileSystemNodes(subFolderNode, subfolder);
        }
    
        // add the files within the folder
        foreach (FileInfo file in currentFolder.GetFiles())
        {
            currentNode.Nodes.Add(file.Name);
        }
    }

    There are of course other considerations.  You may want to consider doing the enumeration of the folders and files in a separate thread or you might want to load only the current set of folders and files and then when a folder node is open do the next lower level.

    0
  • 0
    Thanx man.....this helps a lot.
    0
  • 0

    I got the TreeView working, next step is connect the TreeView to a List View (like a folder browser)....can anyone show me a simple example? this is what I got sofar:

    C#
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Diagnostics;
    namespace Offshore_Supports_Menu
    {
    public partial class ProjectWindow : Form
    {
    public ProjectWindow()
    {
    InitializeComponent();
    }
    
    //Add subFolders to the Project Folder
    private void CreateDirectoryNodes(TreeNode currentNode, DirectoryInfo currentFolder)
    {
    // add all sub-folders within the current folder
    foreach (DirectoryInfo subfolder in currentFolder.GetDirectories())
    {
    TreeNode subFolderNode = currentNode.Nodes.Add(subfolder.Name);
    CreateDirectoryNodes(subFolderNode, subfolder);
    }
    }
    private void ProjectWindow_Load(object sender, EventArgs e)
    {
    //Get the Project Folder
    DirectoryInfo startFolder = new DirectoryInfo(@"F:\Projecten\Offshore Support Templates\2_Projects\10.3459 Ormen Lange II");
    TreeNode rootNode = ProjectTreeView.Nodes.Add(startFolder.Name);
    rootNode.ImageIndex = 2;
    rootNode.SelectedImageIndex = 2;
    CreateDirectoryNodes(rootNode, startFolder);
    rootNode.Expand();
    }
    
    private void ProjectTreeView_AfterSelect(object sender, TreeViewEventArgs e)
    {
    }
    private void ProjectTreeListView_SelectedIndexChanged(object sender, EventArgs e)
    {
    }
    }
    }
    0