Windows Explorer in C#




WindowsExp1.gif

For creating a Windows Explorer type of application we need to design our form in the following manner. Follow the following steps.

  1. Open Visual Studio - > Click on File menu then click on New Project.
  2. Select the language as C# and select Window Application from the New Project Template.
  3. Specify the name for your application as WindowsExplorer and Click OK.
  4. Now you'll get your form open.
  5. Now design the form in the following manner.

    a. 1st take a Panel control and set the dock property to top of the panel control.
    b. Add one more Panel to the Panel and set the Dock property to left.
    c. Add a Label control and a TextBox control to the right side of the 1st panel.
    d. 2nd take a split container control and set the Dock property to Dock in Parent Container by clicking the smart tag of your split container control.
    e. As you added split container by default it has 2 panels in it.
    f. In 1st panel add a TreeView control and set the Dock property to dock in parent container.
    g. In 2nd panel add a listview control and set the Dock property to dock in parent container.
    h. For getting the images we need to add an imagelist control with some images in it (images such as mycomputer, mydocuments, files, folders, cdrom, drives icon image).
    I. Now your form should look like this.

    WindowsExp2.gif

Now we'll start with the coding part of our project.

The first thing is to determine that a particular computer has how many drives so instead of determining it manually we can always make use of DriveInfo class which is there in System.IO namespace it can help you to get all the drives which are currently present in one PC. So there is no need to create a separate program for every PC.

Before doing this we need to create some Treenodes at the class level for Desktop, My Documents and My Computer.
The Code for the following is:

TreeNode root = new TreeNode("Desktop");
TreeNode doc = new TreeNode("My Documents");
TreeNode comp = new TreeNode("My Computer ");
TreeNode drivenode;
TreeNode filenode;
DirectoryInfo dir;
string path = "";

On the form load event we need to write code for adding these nodes to the TreeView and for getting the drives which are present in one PC.

private void Form1_Load(object sender, EventArgs e)
{
listView1.LabelEdit = true;
listView1.FullRowSelect = true;
listView1.Sorting = SortOrder.Ascending;
treeView1.Nodes.Add(root);
doc.ImageIndex = 5;
comp.ImageIndex = 4;
treeView1.Nodes.Add(doc);
treeView1.Nodes.Add(comp);
GetDrives();
}

The GetDrives function is being called in the form load event; declare the function as:

private void GetDrives()
{
DriveInfo[] drive = DriveInfo.GetDrives();
foreach (DriveInfo d in drive)
{
drivenode = new TreeNode(d.Name);
dir = d.RootDirectory;
comp.Nodes.Add(drivenode);
switch (d.DriveType)
{
case DriveType.CDRom:
drivenode.ImageIndex = 8;
break;
case DriveType.Fixed:
drivenode.ImageIndex = 1;
break;
case DriveType.Removable:
drivenode.ImageIndex = 1;
break;
case DriveType.NoRootDirectory:

drivenode.ImageIndex = 5;
break;
case DriveType.Network:
drivenode.ImageIndex = 1;
break;
case DriveType.Unknown:
drivenode.ImageIndex = 2;
break;
}
getFilesAndDir(drivenode, dir);
}
}

The above code will create all drives which are present in the current computer and it will add it under the comp nodes which has been created at the class level. In every case the getFilesAndDir() function will be called in which we are ing the drivenode and directory info.

The coding logic for get all the files and dir under one folder is:

private void getFilesAndDir(TreeNode node, DirectoryInfo dirname)
{
try
{
foreach (FileInfo fi in dirname.GetFiles())
{
filenode = new TreeNode(fi.Name);
filenode.Name = fi.FullName;
getFileExtension(filenode.Name);
node.Nodes.Add(filenode);
}
try
{
foreach (DirectoryInfo di in dirname.GetDirectories())
{
TreeNode dirnode = new TreeNode(di.Name);
dirnode.ImageIndex = 2;
dirnode.Name = di.FullName;
node.Nodes.Add(dirnode);
getFilesAndDir(dirnode, di); //Recursive Functioning
}
}
catch (Exception e1)
{
}
}
catch (Exception e1)
{
}
}

The above code will add all files and folders under one folder using a recursive function but to get the default file icon we need to call the getFileExtension() Method by ing in the argument file name. We can use the same coding logic for getting the files and folders on the desktop as well as from the My Documents.

The coding for the getFileExtension() Method is:

private void getFileExtension(string filename)
{
switch (Path.GetExtension(filename))
{
case ".txt":
case ".rtf":
filenode.ImageIndex = 6;
break;
case ".doc":
case ".docx":
filenode.ImageIndex = 0;
break;
case ".html":
case ".htm":
filenode.ImageIndex = 3;
break;
case ".rar":
case ".zip":
filenode.ImageIndex = 9;
break;
case ".java":
filenode.ImageIndex = 10;
break;
default:
filenode.ImageIndex = 7;
break;
}
}

This will get the file extension and based on the ImageIndex of the specified case will display the image icon for the file and for the rest of the file it will display an unknown icon image file.

Now we to need to write the code for displaying the data in the TreeView and on select of a node we need to display the content of that folder in the ListView. We'll write the code in the AfterSelect event of our TreeView.

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
try
{
listView1.Items.Clear();
TreeNode selectednode = e.Node;
treeView1.SelectedNode.ImageIndex = e.Node.ImageIndex;
selectednode.Expand();
textBox1.Text = selectednode.FullPath;
if (selectednode.Nodes.Count > 0)
{
foreach (TreeNode n in selectednode.Nodes)
{
ListViewItem lst = new ListViewItem(n.Text, n.ImageIndex);
lst.Name = n.FullPath.Substring(13);
MessageBox.Show("List Node : " + lst.Name);
listView1.Items.Add(lst);
}
}
else
{
listView1.Items.Add(selectednode.FullPath, selectednode.Text, selectednode.ImageIndex);
}
}
catch (Exception e1)
{
}
}

Now when you select any node from the TreeView it will display all the folders and the files in the ListView with their defualt icons.

Now we need to write code for the double-click event of the ListView; it should display all the files and folders under that folder. Right-click on the ListView and go to events and double-click on the mouseDoubleClick event of the ListView. This will diplay a listView1_MouseDoubleClick for the ListView.

private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].Selected==true)
{
path = listView1.Items[i].Name;
textBox1.Text = path;
listView1.Items.Clear();
LoadFilesAndDir(path);
}
}
}

The LoadFilesAndDir function will open all the directories and the files under the specified path. The following is the coding logic for it.

private void LoadFilesAndDir(string address)
{
DirectoryInfo di = new DirectoryInfo(address);
try
{
foreach (FileInfo fi in di.GetFiles())
{
listView1.Items.Add(fi.Name, filenode.ImageIndex);
}
try
{
foreach(DirectoryInfo listd in di.GetDirectories())
{
listView1.Items.Add(listd.FullName,listd.Name, 2);
}
}
catch (Exception e1)
{
}
}
catch (Exception e1)
{
}
}

In my further program we'll try to create the coding logic for the forward and backward buttons which are there in the Windows Explorer. Hope you like this example and it will help you to carry out your program.

Untill the next time Enjoy Programming.

Up Next
    Ebook Download
    View all
    Learn
    View all