Recursion to get Directory List and showing it in TreeView.

October 7, 2006

Simple recursion to get all directories inside the specifed directory and then bind it to a TreeView

//Main method inside a Win Form
private static void Main()
TreeView tv=new TreeView();
DirectoryInfo dirInfo = new DirectoryInfo(“d:\\anyDir”);
TreeNode tn=new TreeNode(dirInfo.Name);
ProduceListing(dirInfo, ” “,tn);
tv.Location=new Point(5,5);
tv.Size=new Size(this.Width,this.Height);
private void ProduceListing(DirectoryInfo dirInfo,string Spacer,TreeNode TN)
foreach(DirectoryInfo subDir in dirInfo.GetDirectories())
TreeNode tn=new TreeNode(subDir.Name);
Console.WriteLine(Spacer + Spacer + “{0}”, subDir.Name);
if( subDir.GetDirectories().Length > 0 )
ProduceListing(subDir, Spacer + ” “,tn);



Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: