Click here to Skip to main content
Click here to Skip to main content

Windows Explorer in C#

By , 20 May 2002
 

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
No Biography provided

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membernekimiro4 Apr '12 - 21:32 
Very simple and nice to use
QuestionNavigate to a nodemembermaro0096 May '11 - 10:34 
Great example thank a lot
 
I have just a small question, i would like to add a text box using it as a navigation pane
so i would like to type a folder path then the tree view will auto expand and select the path
 
Do you know how can i do that?
 
Thanks for your help
 
Mike
Generalwindows explolermemberajay1z13 Jan '11 - 19:31 
great example it helps me very much.
Generalimagelist problemmemberpipicuit6 Jan '09 - 13:36 
i am really wondering how did you load or put those XP icons to the imagelist? I tried to load those ico directly from imagelist properties, but when i run the code, those icons look ugly (outline) whereas when i run your code, icons look just perfect.
GeneralRe: imagelist problemmemberRussell Mangel8 Jan '09 - 16:16 
I did nothing special at the time, I just added the icons as you did. However, at that time I was using Visual Studio 2002 (I think that was .NET Framework 1.0).
 
I duplicated your problem by opening the project and converting it to Visual Studio 2008. I was able to fix the problem by fiddling with the imagelist properties.
 
Try this:
 
Modify the properties of ImageList:
 
ColorDepth = Depth32Bit
 
If that doesn't work, try changing:
 
ImageSize = 32
 
If this doesn't solve your problems, I suggest that you do some googling for treeview and imagelist icon problems. Many people have been complaining about this exact problem. The problem was not a problem in 2002, but now it is, so Microsoft obviously changing some things in imagelist.
 
Russ.
 
Russell Mangel
Las Vegas, NV

QuestionDummy Windows Explorer and Windows Context MenumemberMember 36436946 Sep '08 - 22:46 
Hi
 
Thanks for your code. This is really helpful. But is there any way to cloning the windows explorer. I mean to say The listview which can show the windows context menu on reght clicking on any file or folder. I am not talking about any custom context menu. But the same as windows context menu by useing any Win32API or Shell32API?
 
Debashis Pal
Wipro Technologies

GeneralWeb Based VersionmemberJim Brooks28 May '08 - 12:21 
Here is a web control that does basically the same thing:
http://www.DigitalTools.com/GVT.aspx[^]
 
Jim

GeneralGreat example but...membersharpiesharpie16 Feb '07 - 13:32 
Your way of getting the drives was all wrong...you just assumed that the floppy drive would be A, and the fixed ones would be C,D, therefor it showed a floppy drive named A in your program even though i don't even have one.
 
you can use the DriveInfo class in order to get the drive type.
GeneralRe: Great example but...memberRussell Mangel18 Feb '07 - 11:23 
Thanks for the comments.
 
When I wrote this code back in 2002 (wow it's been 5 years), for .NET version 1.0 there was no pure c# way to get drive types except for using wmi namespace and wmi is really slow, so I just hard-coded them.
 
I really should post a new version for .NET 2.0. As you mentioned there is a new DriveInfo class in .NET 2.0 that does have drive type enumeration.
 
Faster performance for getting Files and Directories.
 
When dealing with directories that have hundreds of thousands of files, it is more efficient to get the files and directories in one pass, you can use FileSystemInfo class for this. However, when calling the attributes property (to determine if the object is a file or folder), then performance is terrible.
 
For extreme File and Directory performance, use pinvoke and the FindFirstFile or(FindFirstFileEx) WinAPI method.
 
Have a good day.
 
Russ
 

 
Russell Mangel
Las Vegas, NV

GeneralRe: Great example but...memberProgramm3r18 Nov '07 - 22:26 
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: ^_^

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

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