SIGN UP MEMBER LOGIN:    
ARTICLE

Directory Picker in C#

Posted by Mike Gold Articles | Windows Controls C# February 07, 2001
A directory tree control let you select files or directories from any file structure on your computer system.
Reader Level:
Download Files:
 

This article illustrates the use of a treeview in a directory tree user control.  You can use this control to visually select files or directories from any file structure on your computer system. 

The treeview has been a common way to display hierarchies of data.  In C# it's fairly easy to manipulate a tree because the design of the treeview is based on a recursive Node Structure:

This design was drawn using WithClass 2000 UML Design Tool

 The treeview uses these node structures for handling most of its data population.  The images from the tree view come from an ImageList component.  The ImageList  must be constructed seperately, and the icons used in the imagelist are added to the Images Collection from pictures created in an image editor (like mspaint or something):

The ImageList is then associated to the TreeView via the ImageList property:

When nodes are constructed, they are assigned indexes that point to a particular image in the list.

Now let's take a good look at our directory tree user control.  The code for populating the directory nodes takes advantage of the Directory class in the System.IO namespace.  A tree is built by first constructing the node with the name, image index, and image index when a node is selected.  Then the node is added to the parent node.

protected void PopulateNode(TreeNode aNode)
{
this.Cursor = Cursors.WaitCursor; // Set up the wait cursor
string strDir = BuildDirectory(aNode); // Build the directory tree string from the tree node
Directory Dir = new Directory(strDir); // Use the Directory class to get the files and directory names in the directory
int count = 0;
// Get each directory name and add it as a directory folder tree node
for (int i = 0; i < Dir.GetDirectories().Length; i++)
{
TreeNode ChildNode =
new TreeNode(Dir.GetDirectories()[i].Name, 1,0);
aNode.Nodes.Add(i, ChildNode);
count++;
}
// Get each file name and add it as a file tree node
for (int i = 0; i < Dir.GetFiles(m_strFilter).Length; i++)
{
TreeNode ChildNode =
new TreeNode(Dir.GetFiles(m_strFilter)[i].Name, 2,2);
aNode.Nodes.Add(i+count, ChildNode);
}
aNode.Expand();
// show the children nodes
this.Cursor = Cursors.Arrow; // restore the cursor
}

The function BuildDirectoryTree is used to construct the string needed to go to the file directory being populated and is shown below:

protected string BuildDirectory(TreeNode aNode)
{
TreeNode theNode = aNode;
string strDir = "";
// Cycle through the node and all its parents to build the full path of the directory
while (theNode != null)
{
if (theNode.Text[theNode.Text.Length - 1] != '\\')
{
strDir = "\\" + strDir;
}
strDir = theNode.Text + strDir;
theNode = theNode.Parent;
}
return strDir;
}

The event AfterSelect on a TreeView, can be used to trap the selection event of a particular node. In the case of the directory tree component, we use this selection event  as an opportunity to populate an empty node:

public void treeView1_AfterSelect (object sender, System.WinForms.TreeViewEventArgs e)
{
// check to see if the selected node is already populated
if (e.node.Nodes.Count > 0 || e.node.ImageIndex == 2)
{
return;
}
// The node is not populated, so populate the selected node
PopulateNode(e.node);
}

This control can be used as a starting point for creating explorer like applications.  The Path and FileName
properties serve as outputs of the control for obtaining directory information selected by the user.  The following code illustrates how the information can be obtained by the user after a button is pressed.

protected void button1_Click (object sender, System.EventArgs e)
{
label3.Text = dirTree1.Path;
label4.Text = dirTree1.FileName;
}

In future articles we will explore the ability of trapping events directly from the control rather than from a button press.  Please write or post comments in the discussion forum if you have any ideas on expanding the capability of this control.

Login to add your contents and source code to this article
share this article :
post comment
 

Did you ever post a reply to the first message submitted and if you did could you please forward your answer on to me as well. I am not having any luck removing the full path from the nodes without causing an exception at this time. Here is the first message submitted in case you have forgotten: "hi, currently each node name is given the full path to the folder/file. is it possible to only have each node labelled by the folder/file name alone and not the full path. " Another question I have is there a way to go about programmatically selecting more than one treenode at a time, e.g. like a multi-select. Thanks, Adrian

Posted by A D Oct 01, 2007

hi, currently each node name is given the full path to the folder/file. is it possible to only have each node labelled by the folder/file name alone and not the full path. i.e. similar to how it is displayed in the image above?? thanks robert

Posted by robert Jul 04, 2007
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor