How to Create Directory and File tree in C#

Posted By  RRaveen On 12 Jul 2010 23:07:18
emailbookmarkadd commentsprint
No of Views:3369
Bookmarked:0 times
Votes:0 times

Introduction

In this tips I have given, create directory and file detail as tree view in C#, I have seem so many questions in asked in forums, how to get the  directory and file information in  recursive manner.Hence I have created a simple screen with a treeview and label. Then compose the code to build directory tree.

Implementation

I have created two methods for this operation. First one for build directory tree and second one for files trees.

private void BuildTree(string directpath){DirectoryInfo dirInfo = new DirectoryInfo(directpath);FileSystemInfo[] collectionOfFAndD = dirInfo.GetFileSystemInfos();TreeNode parentnode = null;parentnode = new TreeNode(dirInfo.Name);parentnode.Tag = dirInfo.FullName;this.treeView1.Nodes.Add(parentnode);foreach (FileSystemInfo var in collectionOfFAndD){if (var.Attributes == FileAttributes.Directory){TreeNode node = new TreeNode(var.Name);node.Tag = var.FullName;parentnode.Nodes.Add(node);BuildFileTree(var.FullName, node);}else{TreeNode node = new TreeNode(var.Name);node.Tag = var.FullName;parentnode.Nodes.Add(node);}}}

second method support add files to treeNode.

private void BuildFileTree(string fileName, TreeNode parentnode){DirectoryInfo dirInfo = new DirectoryInfo(fileName);FileSystemInfo[] collectionOfFAndD = dirInfo.GetFileSystemInfos();foreach (FileSystemInfo var in collectionOfFAndD){if (var.Attributes == FileAttributes.Directory){TreeNode node = new TreeNode(var.Name);node.Tag = var.FullName;parentnode.Nodes.Add(node);BuildFileTree(var.FullName, node);}else{TreeNode node = new TreeNode(var.Name);node.Tag = var.FullName;parentnode.Nodes.Add(node);}}}

The call parent method within the form load event to create treeview with directory

private void Form1_Load(object sender, EventArgs e){BuildTree(@"C:\images");}

 now when you are click file node or directory node, you want to know full path and size of the file. so i have added code within after select event for treeview.

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e){FileInfo info = new FileInfo(e.Node.Tag.ToString());if (info.Attributes != FileAttributes.Directory){label1.Text = "Path:" + info.FullName + " \n Size:" + info.Length + " bytes";}else{label1.Text = "Path:" + info.FullName;}}

 When you are select any node in treeview you able to get path and size of the file. i hope this is help to you all.

Sign Up to vote for this article
Other popular Tips/Tricks
    In this tip, I will explain how to use the BigInteger in C# 4.0.The BigInteger is new data type in .NET 4.0.
    Published Date : 22/Sep/2011
    In this tips,I am going to discuss about two important thing about Split function of String class. Split function of the string class split the string in array of string.
    Published Date : 29/Jul/2011
    The Facebook is very popular and it has great support to other development Languages by Facebook SDKs.In this tips, I going to explain how to upload Videos to Facebook through C# with few lines of code.
    Published Date : 13/Jun/2011
    In this tip, i will share with you , how to format time HH:MM:SS from seconds in C#.
    Published Date : 25/Feb/2011
    Enums in dot net programming is a great facility and we all used it to increase code readability. In earlier version of .NET framework we don’t have any method anything that will check whether a value is assigned to it or not. In C# 4.0 we have new static method called HasFlag which will check that particular value is assigned or not.
    Published Date : 01/Jan/2011
Comments
By:RRaveenDate Of Posted:5/30/2010 9:55:12 PM
Yes
Yes, There few type of Sync providers,ADO.NET CE is give to support to sync between to .NET CF and .NET Framework application data. its mean say you have main sql server and sql ce you able to sync between do easily. If you want to use the File System then you able to do the custom coding.but have support. if you have any more question, please post in message board codegain experts will try to give answers. thank you
By:CarnDate Of Posted:5/30/2010 9:06:51 PM
Can be used in mobile devices?
Hi There, I am just wondering is this file sync provider able to be used in mobile ? I mean like running in mobile environment. Coz from wat i found out from the net saying that sync framework only able to run in Desktop. Correct me if I m wrong. Also, if it is running at desktop, for the file path i put \ as the device directory, it appears to me an error showing method missing exception, or the path couldnt be found. For desktop running syncing both folder with the same code, no errors appeared. Do you know any solution of this? Thanks
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top