Click here to Skip to main content
Licence 
First Posted 20 May 2002
Views 139,857
Bookmarked 75 times

Windows Explorer in C#

By | 20 May 2002 | Article
This is the first in a series of articles demonstrating how to create an

Sample Image

Introduction

This is the first in a series of articles demonstrating how to create an Explorer-like application. The program uses the following controls: TreeView, ListView, Splitter, MainMenu, StatusBar, Panel, ImageList, and ColumnHeaders for the ListView. Many of the properties are set using the designer, so I would suggest that you download the project, unless of course you are not a beginner.

The program demonstrates the following:

  • How to read files and directories from disk, using the DirectoryInfo instance class.
  • How to populate nodes in a TreeView Control.
  • How to populate the ListView Control.
  • How to use a basic StatusBar.
  • How to use the After_Select Event of the TreeView Control.
  • How to use the Item_Activate Event of the ListView Control.
  • How to use the Load Event of the Form itself.

What I think is best about this program

  1. It is a simple design, which should help you to learn how to use the TreeView Control, It took four revisions.
  2. Compared to many of the other sample or demo programs available,  it loads files and directories fast!
  3. The program only loads the necessary files and folders, not the entire directory tree like many demos do.
  4. The TreeView's Tag property is entirely unused in this implementation, leaving you a place to put something.
  5. There is only one event, and two methods which drive the TreeView control.
  6. This program uses a minimal amount of string chopping, unlike many other demos in it's class.

In closing

This program is designed to show the basics to the beginner, experts will have to wait until I  become an expert! There are too many hard decisions to make when designing an Explorer Interface. So I have made some trade offs to keep the code as readable as possible. I plan to remove the "hard coded" stuff in future releases. 

One of the hardest decisions was to start the root directories at the Drive level instead of nesting under the Desktop\MyComputer nodes, which is what Windows Explorer does. You will also notice that I have turned off the "+" and "-" indicators on the TreeView, this was by design as the code to make this work properly is pretty complex for a beginner, not too mention doubling the code. 

I am disappointed that .NET does not have an easy way (maybe there is) too determine the type of drives attached to the computer. I wanted to use PInvoke, but I figured I would do that in another series or update.

If anyone has any comments please send email: russell@tymer.net

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Russell Mangel

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembernekimiro21:32 4 Apr '12  
QuestionNavigate to a node Pinmembermaro00910:34 6 May '11  
Generalwindows exploler Pinmemberajay1z119:31 3 Jan '11  
Generalimagelist problem Pinmemberpipicuit13:36 6 Jan '09  
GeneralRe: imagelist problem PinmemberRussell Mangel16:16 8 Jan '09  
QuestionDummy Windows Explorer and Windows Context Menu PinmemberMember 364369422:46 6 Sep '08  
GeneralWeb Based Version PinmemberJim Brooks12:21 28 May '08  
GeneralGreat example but... Pinmembersharpiesharpie13:32 16 Feb '07  
GeneralRe: Great example but... PinmemberRussell Mangel11:23 18 Feb '07  
GeneralRe: Great example but... PinmemberProgramm3r22:26 18 Nov '07  
Something like this ....
 
        private void Form1_Load(object sender, EventArgs e)
        {
            // This routine adds all computer drives to the root nodes of treeView1 control
            /* Get all the logical drives on the computer */
            DriveInfo[] aDrives = DriveInfo.GetDrives();
 
            treeView1.BeginUpdate();
            foreach (DriveInfo d in aDrives)
            {      
                //TreeNode dnMyDrives = new TreeNode(strDrive.Remove(2, 1));
                TreeNode dnMyDrives = new TreeNode(d.Name);
          
                switch (d.DriveType)
                {                    
                    case DriveType.CDRom:
                        dnMyDrives.SelectedImageIndex = 2;
                        dnMyDrives.ImageIndex = 2;
                        break;
                    case DriveType.Fixed:
 
                        // The next statement causes the treeView1_AfterSelect Event to fire once on startup.
                        // This effect can be seen just after intial program load. C:\ node is selected
                        // Automatically on program load, expanding the C:\ treeView1 node.
                        treeView1.SelectedNode = dnMyDrives;
                        dnMyDrives.SelectedImageIndex = 1;
                        dnMyDrives.ImageIndex = 1;
 
                        break;
                    case DriveType.Removable:
                        dnMyDrives.SelectedImageIndex = 0;
                        dnMyDrives.ImageIndex = 0;
                        break;
                    case DriveType.Network:
                        dnMyDrives.SelectedImageIndex = 5;
                        dnMyDrives.ImageIndex = 5;
                        break;
                    default:
                        dnMyDrives.SelectedImageIndex = 3;
                        dnMyDrives.ImageIndex = 3;
                        break;
                }
 
                treeView1.Nodes.Add(dnMyDrives);
            }
            treeView1.EndUpdate();
        }

 

The only programmers that are better that C programmers are those who code in 1's and 0's Green Alien | [Alien]

Smile | :) Programm3r

My Blog: ^_^

QuestionRe: Great example but... PinmemberMrColeyted12:01 23 Feb '08  
GeneralC# Windows Explorer Pinmembertdittmer0:56 5 Jan '07  
GeneralRe: C# Windows Explorer PinmemberRussell Mangel20:38 5 Jan '07  
GeneralWindows Explorer in C# PinmemberSagar Joshi2:44 25 May '06  
GeneralDoesn't sort the directory names PinmemberLyle M4:33 9 Feb '06  
Generalshowplusminus Pinmembersaid_tambal2:58 19 Sep '04  
GeneralNice Example PinmemberRoger Wright22:42 10 May '03  
GeneralRe: Nice Example PinmemberRussell Mangel19:56 8 Jun '03  
GeneralRe: Nice Example PinmemberJonathan de Halleux0:02 23 Oct '03  
GeneralRe: Nice Example PinmemberChris Walsh15:09 4 Apr '04  
GeneralSmall fix .... PinmemberDejan Petrovic17:00 10 Jun '02  
GeneralRe: Small fix .... PinmemberRussell Mangel20:03 8 Jun '03  
GeneralRe: Small fix .... Pinmemberschoeneck2:55 28 Jan '04  
GeneralRe: Small fix .... Pinmemberpfievhn16:26 3 Jun '06  
GeneralSmall fix to your small fix... PinmemberOICU81TOO9:31 28 Nov '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 21 May 2002
Article Copyright 2002 by Russell Mangel
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid