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.
Anthony Trudeau10 pointsPosted Oct 12, 2007, 8:20 AM EDT
Here's a simple example to enumerate the file system:
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.
Gerben8 pointsPosted Oct 14, 2007, 10:34 PM EDT
Gerben8 pointsPosted Oct 21, 2007, 3:48 AM EDT
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:
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) { } } }