Reader Level:
ARTICLE

Step by Step Guide for Creating Your Own "Windows Explorer"

Posted by Chitkaran Singh Articles | Windows Forms C# December 27, 2007
This article explains a step by step procedure for creating an application similar to Windows Explorer.
  • 0
  • 0
  • 31013
Download Files:
 

Objectives

  1. To create a Windows application which will display the system drives, files and folders.
  2. 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.

  1. Fetched folders and files from a specified dive.
  2. Obtained a list of all the available drives in the computer system.
  3. 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

  1. Drag and drop the "imagelist" control from the toolbox.
  2. Select the "images" property and click on "collection".
  3. In the collections box, first select the image to be placed next to the files (selected index will be 0).
  4. Then select the image to be placed next to the folder when it is closed (selected index will be 1).
  5. And finally, select an image that will be placed next to the folder when it is opened (selected index will be 2).

Now select the "treeview" control and on the properties tab, select the "imagelist" property and then select the "imagelist" from the dropdown list.

Now we need to slightly modify the above code to place the respective images next to the nodes.

private void button2_Click(object sender, EventArgs e)
{
     treeView1.Nodes.Clear();
     int imagindex = 0;
     string[] drives = Environment.GetLogicalDrives();
     foreach (string drv in drives)
     {
          imagindex=1;
          TreeNode node = new TreeNode(drv,imagindex,2);
          //node.Text = drv;
          treeView1.Nodes.Add(node);
          filldrives(drv, node, 0);
     }
}
private void filldrives(string drv,TreeNode parent,int level)
{
     try
    
{
          level++;
          if (level > 2)
          return;
          System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(drv);
          if (!dir.Exists)
          return;
          foreach (System.IO.DirectoryInfo dirinfo in dir.GetDirectories())
          {
               TreeNode child = new TreeNode(dirinfo.Name,1,2);
               child.Text = dirinfo.Name;
               parent.Nodes.Add(child);
               filldrives(child.FullPath, child, level);
          }
          foreach (System.IO.FileInfo fileinfo in dir.GetFiles())
          {
               TreeNode child = new TreeNode();
               //child.Text = dirinfo.Name;
               parent.Nodes.Add(child);
               filldrives(child.FullPath, child, level);
          }
      }
      catch (Exception ex)
      {
           ex.ToString();
 
     }
}

When the above code snippet executes, the following images are placed next to the nodes.

Node Index Image
FOLDER(closed) imageindex=1 folderclosed
FOLDER(open) imageindex=2 folderopen
FILE imageindex=0 textimage

Objective 1 Achieved

Now that we have achieved the first objective, let us move on to the next one.

To display the file contents we need a RichText Box. Drag and drop a RichTextBox control from the toolbox and place it on the Panel.

Step 1

The very first step is to differentiate if the user has clicked a file or a folder, as we are not attempting to dispaly a folder's contents in the RichTextBox.

try
{
     System.IO.StreamReader rstr = info.OpenText();
     string[] sline = new string[65535];
     // max lines a text box can hold
     int count = 0;
     if (rstr != null)
     {
          count = 0;
          sline[count++] = rstr.ReadLine();
          while (sline[count - 1] != null)
          {
               sline[count++] = rstr.ReadLine();
               if (count >= 65536) break;
          }
          rstr.Close();
          string[] finalSize = new string[count];
          // find exact size of line array otherwise blank
          // blank lines will show up in editor
          for (int i = 0; i < count; i++)
          finalSize[i] = sline[i];
          richTextBox1.Lines = finalSize;
      }
}
catch
{
     return;
}

This code will check if the file can be opened for the RichTextBox or not. The "TRY" and "CATCH" blocks are applied to test this. If the file cannot be opened, an exception is throws and hence the catch block is executed.

Step 2

The next step is to show the contents of the selected file in the RichTextBox and to display some of its attributes.

  1. Creation date and time
  2. Extension
  3. Last access date and time 

System.IO.StreamReader rstr = info.OpenText();

string[] sline = new string[65535];

// max lines a text box can hold

int count = 0;

if (rstr != null)

{

     count = 0;
     sline[count++] = rstr.ReadLine();
     while (sline[count - 1] != null)
     {
          sline[count++] = rstr.ReadLine();
          if (count >= 65536) break;
     }
     rstr.Close();
     string[] finalSize = new string[count];
     // find exact size of line array otherwise blank
     // blank lines will show up in editor
     for (int i = 0; i < count; i++)
     finalSize[i] = sline[i];
     richTextBox1.Lines = finalSize;
}

This code snippet shows the contents of the selected file in the RichTextBox and the specified file attributes.

string path = treeView1.SelectedNode.FullPath;

System.IO.FileInfo info = new System.IO.FileInfo(path);

create_date_text.Text = info.CreationTime.ToString();         
textBox1.Text = info.Extension.ToString();                           

textBox2.Text = info.LastAccessTime.ToString();

Congratulations, Objective 2 achieved

COMMENT USING

Trending up