Objectives
-
To create a Windows application which will display the system drives, files and folders.
-
If the file is selected from the list, the contents of the file is shown in a TextBox in ReadOnly mode.
List of Files
To begin we need to have some functions and code which can retrieve the directory list from a specified drive.
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("C:\\");
foreach (System.IO.FileInfo f in dir.GetFiles("*.*"))
{
listbox1.Items.Add(f.Name);
}
The above function displays the list of all the files in a ListBox control.
List of Folders
The below function displays the list of all the folders in the ListBox control.
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("C:\\");
foreach (System.IO.DirectoryInfo f in dir.GetDirectories())
{
lstFiles.Items.Add(f.Name);
}
List of Logical Drives
Now we will expand functionality to allow us to select any available system drive and then to display its folders and files.
treeView1.Nodes.Clear();
string[] drives = Environment.GetLogicalDrives();
foreach (string drv in drives)
{
TreeNode node = new TreeNode();
node.Text = drv;
treeView1.Nodes.Add(node);
}
The above set of commands gets a list of all the available logical drives and shows them in the Tree View control.
Still, the display of the Tree View is not as useful as it could be. The TreeView shows the Drives but it does not show the files and folders contained in them. Our next step will be to have a function that is able to display the drives along with their related files and folders.
List of Logical Drives with their Files and Folders
private void filldrives(string drv,TreeNode parent,int level)
{
try
{
level++;
if (level > 4)
return;
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(drv);
if (!dir.Exists)
return;
foreach (System.IO.DirectoryInfo info in dir.GetDirectories())
{
TreeNode child = new TreeNode();
child.Text = info.Name;
parent.Nodes.Add(child);
filldrives(child.FullPath, child, level);
}
foreach (System.IO.FileInfo fileinfo in dir.GetFiles())
{
TreeNode child = new TreeNode();
child.Text = fileinfo.Name;
parent.Nodes.Add(child);
filldrives(child.FullPath, child, level);
}
}
catch (Exception ex)
{
ex.ToString();
}
}
The above snippet will populate the drives with their respective folders and files. The above function will be called in the following manner, noting the additional "filldrives" function call.
treeView1.Nodes.Clear();
string[] drives = Environment.GetLogicalDrives();
foreach (string drv in drives)
{
TreeNode node = new TreeNode();
node.Text = drv;
treeView1.Nodes.Add(node);
filldrives(drv, node, 0);
}
Now lets see what we have achieved until now.
-
Fetched folders and files from a specified dive.
-
Obtained a list of all the available drives in the computer system.
-
Automatically retrieved the folders and files in each directory (Tree View)
There is still a simple but critical thing missing: the ability for a user to distinguish between a drive, folder or file. For this we need to assign appropriate icons to the respective nodes so that a user is able to distinguish between them.
Applying Images to the Tree Nodes
This can be achieved with the help of a control "imagelist" which includes a list of all the images to be used.
Steps
-
Drag and drop the "imagelist" control from the toolbox.
-
Select the "images" property and click on "collection".
-
In the collections box, first select the image to be placed next to the files (selected index will be 0).
-
Then select the image to be placed next to the folder when it is closed (selected index will be 1).
-
And finally, select an image that will be placed next to the folder when it is opened (selected index will be 2).

Gowtham RajamanickamPosted Mar 27, 2015, 2:38 PM
good....
SUPRATIM DASPosted Jun 28, 2009, 11:20 PM
Thank you !
hugh adamsonPosted Feb 25, 2008, 6:49 PM
Corruptdownload
Chitkaran SinghPosted Jan 8, 2008, 11:56 AM
Hehe...Poor You !!! Things that you dont understand are not necessarily bogus.. So just go through it and try to understand whats written in it.. And if u need any help then do let me know, I will be more than happy to assist you in understandign the language well.. I really like to teach people.. especially POOR PEOPLE.. !!!