
How to list directories in a TreeView control?
Hi Jonathan,
I don't believe that there are any "managed" controls that do what you
require explicitly. However, it is quite an easy task to create something
yourself that will do the job.
I have included two sections of code that may be helpful:
1) A static class called DirectoryTreeHelper
2) An example of how you might use it in a form to achieve the result you
are looking for.
The included code should work. Although it doesn't add images to your
TreeView, you can remedy that simply enough by enhancing the
DirectoryTreeHelper class. Perhaps you could improve it and incorporate it
within a control of your own?
If this is no good, try looking in the Win32 API for something that exists
already.
Bryce
---------
Usage:
1) Call the InitializeTreeView() method once for each TreeView in your form
during the form initialization.
2) Add the ExpandNode(TreeNode) method to a BeforeExpand event handler
assigned to each TreeView in your form
// Snippets from "your" code showing how to use the static
DirectoryTreeHelper class
public class DirectorySelectForm : System.Windows.Forms.Form
{
public DirectorySelectForm()
{
// This call is required by the Windows Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitializeComponent
call
// IMPORTANT! These statements initialize the TreeViews with
root nodes representing the logical drives on the host system.
// You could include them here, or somewhere else in your
code if you prefer.
DirectoryTreeHelper.InitializeTreeView(treeView1);
DirectoryTreeHelper.InitializeTreeView(treeView2);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
// If you are using VisualStudio, you should be able to
assign these event handlers using the designer.
this.treeView1.BeforeExpand += new
System.Windows.Forms.TreeViewCancelEventHandler(
this.TreeView_BeforeExpand);
this.treeView2.BeforeExpand += new
System.Windows.Forms.TreeViewCancelEventHandler(
this.TreeView_BeforeExpand);
}
private void TreeView_BeforeExpand(object sender,
System.Windows.Forms.TreeViewCancelEventArgs e)
{
DirectoryTreeHelper.ExpandNode(e.Node);
}
// etc
}
// The DirectoryTreeHelper class
using System;
using System.IO;
using System.Windows.Forms;
namespace MyNamespace
{
public class DirectoryTreeHelper
{
public static void InitializeTreeView(TreeView treeView)
{
treeView.Nodes.Clear();
string[] drives = Directory.GetLogicalDrives();
DirectoryInfo[] directoryInfo = new
DirectoryInfo[drives.Length];
for(int i = 0; i < drives.Length; i++)
{
directoryInfo[i] = new DirectoryInfo(drives[i]);
}
PopulateNodes(treeView.Nodes, directoryInfo);
}
public static void ExpandNode(TreeNode node)
{
// If placeholder then check for subdirectories.
if((node.Nodes.Count == 1) && (node.Nodes[0].Tag
== null))
{
node.Nodes.Clear();
try
{
PopulateNodes(node.Nodes,
((DirectoryInfo)node.Tag).GetDirectories());
}
catch(System.IO.IOException e)
{
DialogResult result =
MessageBox.Show(e.Message, "IO Exception",
MessageBoxButtons.RetryCancel,
MessageBoxIcon.Error);
if(result == DialogResult.Retry)
{
node.Nodes.Add(""); // Add the
placeholder again
ExpandNode(node);
}
}
catch(System.Security.SecurityException e)
{
MessageBox.Show(e.Message, "Security
Exception",
MessageBoxButtons.OK,
MessageBoxIcon.Stop);
}
}
}
private static void PopulateNodes(TreeNodeCollection nodes,
DirectoryInfo[] directoryInfo)
{
foreach(DirectoryInfo info in directoryInfo)
{
TreeNode node = nodes.Add(info.Name);
node.Tag = info;
// Add a placeholder node to force display of
PlusMinus button
node.Nodes.Add("");
}
}
}
Quote:
}
Quote:
> Hi,
> Is there any controls (preferrably, part of the Windows distribution) that
> will list all the file system's directories in a TreeView-like control?
I'm
> planning to put 2 such controls side-by-side in a single form, to allow
> users to select 2 directories.
> Would appreciate any pointers on this.
> Thanks.
> Jonathan