Click here to Skip to main content
13,045,790 members (45,742 online)
Click here to Skip to main content
Add your own
alternative version


240 bookmarked
Posted 25 Jun 2006

A Windows Explorer in a user control

, 15 Mar 2007
Rate this:
Please Sign up or sign in to vote.
Functionality of Windows Explorer in a user control using C#

Sample Image - ExplorerTree.jpg


Idea: Functionality of windows explorer wrapped up as a control. User can browse through all the folders in the computer including network folders and select the path.

The Long story, while working on a photo editing software i frequently needed to test different images from different folders. i have to call an open dialog then browse for the folder and then select the file. 4 clicks well not too much but if you have to test 10 images in the same folder same process have to be repeated for all of them, more if you have 10 images in different folders well 10 X 4 clicks, i call it Multiple Mouse Clicks For Single Operation (MMCFSO). Explains why the whole software world relates reducing MMCFSO with the ease of use.

MMCFSO is not just needed for a imaging application. Where ever you have to select multiple files from multiple destinations you need this. So i think an effort to create a windows explorer user control is worth it. Here is the result.

To hold your interest, here is how the ExplorerTree control looks like.

A UserControl is an object which can display and accept information from a user. It usually fulfills a specific functions of input or output. What is our ExplorerTree control going to do ?

1. Display a treeview of all the folders like Windows Explorer
2. It will take user input as mouse click and expand the folder
3. Output will be the path of the current selection

The target was to create a user friendly control which simulates windows explorer with the following

  • Rich functionality: It should be rich in functionality and have all the capabilities of a windows explorer
    • Browse all possible folders my documents, desktop, network neighbourhood etc
    • Go, up, back, next features like Windows explorer
    • Enable /disable the folders he wants to see aka My Documents/ My Network/My Favorites etc
  • Nice user interface: It should look professional and neat
  • Reusability: The feature should work as a control user can drop it in any form and have a readymade functionality of a windows explorer.
  • Extensibility: User should be able to add shortcut to frequently used folders

Without any furthur ado, lets jump into how each of these targets are met.


Customizable Toolbar and addressbar




  • Create the explorer like tree-view for all the folders
  • Add shortcut to frequently used folders , this is also to extend the feature of ExplorerTree
  • The "Go" button, "Go Back", "Go Forward" and "Go Up" are self explainatory and simulates the Windows Explorer functions
  • Refresh will re-create the explorer tree
  • Home will set the current directory to the executable path of ExplorerTree


  • Selected Path is the current selection by the user and also the output of the ExplorerTree control
  • ShowMyDocuments
  • ShowMyFavorites
  • ShowMyNetwork


  • PathChangedEvent is fired when ever the path of the ExplorerTree is changed

The Code behind : explore my computer

string [] drives = Environment.GetLogicalDrives();
foreach(string drive in drives)

    nodeDrive = new TreeNode();
    nodeDrive.Tag = drive;

    nodeDrive.Text = drive ;

    //Determine icon to display by drive
        case 2:
            nodeDrive.ImageIndex = 17;
            nodeDrive.SelectedImageIndex  = 17;
        case 3:
            nodeDrive.ImageIndex = 0;
            nodeDrive.SelectedImageIndex  = 0;
        case 4:
            nodeDrive.ImageIndex = 8;
            nodeDrive.SelectedImageIndex = 8;
        case 5:
            nodeDrive.ImageIndex = 7;
            nodeDrive.SelectedImageIndex = 7;
            nodeDrive.ImageIndex = 0;
            nodeDrive.SelectedImageIndex = 0;

        //add dirs under drive
        if (Directory.Exists (drive))
            foreach(string dir in Directory.GetDirectories(drive))
                dir2 = dir;
                node = new TreeNode();
                node.Tag = dir;
                node.Text = dir.Substring(dir.LastIndexOf(@"\") + 1);
                node.ImageIndex = 1;

    catch(Exception)    //error just add blank dir

The Code behind : explore my folders

if (ShowMyDocuments)
    nodemd = new TreeNode();
    nodemd.Tag = Environment.GetFolderPath(
    nodemd.Text = "My Documents";
    nodemd.ImageIndex = 9;
    nodemd.SelectedImageIndex = 9;

if (ShowMyNetwork)
    nodemyN = new TreeNode();
    nodemyN.Tag = "My Network Places";
    nodemyN.Text = "My Network Places";
    nodemyN.ImageIndex = 13;
    nodemyN.SelectedImageIndex = 13;

    nodeEN = new TreeNode();
    nodeEN.Tag = "Entire Network";
    nodeEN.Text = "Entire Network";
    nodeEN.ImageIndex = 14;
    nodeEN.SelectedImageIndex = 14;

    nodeNN = new TreeNode();
    nodeNN.Tag = "Network Node";
    nodeNN.Text = "Network Node";
    nodeNN.ImageIndex = 15;
    nodeNN.SelectedImageIndex = 15;

if (ShowMyFavorites)
    nodemf = new TreeNode();
    nodemf.Tag = Environment.GetFolderPath(
    nodemf.Text = "My Favorites";
    nodemf.ImageIndex = 26;
    nodemf.SelectedImageIndex = 26;

The Go Back and Go Forward buttons are handled using a listview as shown below, the selected is the current path and go back will just move the cursor to one up and go next to one down.

The Code behind : Go Back and Go Forward

private void UpdateListGoBack() 
 if ((listView1.Items.Count >0)&&
     int i=0;
    for (i = 0;i< listView1.Items.Count;i++)
        if (String.Compare(listView1.Items[i].SubItems[1].Text,"Selected")==0)
            if (i != 0)
                listView1.Items[i - 1].SubItems[1].Text = "Selected";
                txtPath.Text =listView1.Items[i - 1].Text;
        if (i != 0)
            listView1.Items[i].SubItems[1].Text = " -/- ";
private void UpdateListGoFwd()
    if ((listView1.Items.Count >0) && 
         listView1.Items[listView1.Items.Count -1 ].SubItems[1].Text, 
    int i=0;
    for (i = listView1.Items.Count-1;i >= 0;i--)
        if (String.Compare(listView1.Items[i].SubItems[1].Text,"Selected")==0)
            if (i != listView1.Items.Count) 
                listView1.Items[i + 1].SubItems[1].Text = "Selected";
                txtPath.Text =listView1.Items[i + 1].Text;   

        if (i != listView1.Items.Count-1)
            listView1.Items[i].SubItems[1].Text = " -/- ";

Update list function : Go Back and Go Forward

private void updateList(string f)
    int i=0;
    ListViewItem listviewitem;        // Used for creating listview items.

    int icount =0;
    icount = listView1.Items.Count + 1;
        if (listView1.Items.Count> 0)
            if (String.Compare(listView1.Items[listView1.Items.Count-1].Text, f)==0)
        for (i = 0;i < listView1.Items.Count;i++)
            listView1.Items[i].SubItems[1].Text = " -/- ";
        listviewitem = new ListViewItem(f);
        listviewitem.Tag = f;
    catch(Exception e)

Explore Network Places

When i started porting one of my old existing Visual Basic Server enumeration function for the network places i came across a brilliant article by Rob Manderson here , He had ported successfully NETRESOURCE class in c sharp. I have used his class to furthur get all the different kinds of network resources and shared folders in the systematic way, how Windows explorer does it. His article actually helped me to do what i wanted to do without worrying about the enumeration process.

ServerEnum servers = new ServerEnum(ResourceScope.RESOURCE_GLOBALNET,

foreach    (string    s1 in servers)
    string s2="";

    if((s1.Length <6)||(String.Compare(s1.Substring(s1.Length-6,6),"-share")!=0))
        s2 = s1;//.Substring(s1.IndexOf("\\",2));
        nodeNN = new TreeNode();
        nodeNN.Tag =  s2;
        nodeNN.Text = s2.Substring(2) ;
        nodeNN.ImageIndex = 12;
        nodeNN.SelectedImageIndex = 12;
        foreach    (string    s1node in servers)
            if (s1node.Length >6)
                    if (s2.Length <=s1node.Length )

            if (String.Compare(s1node.Substring(0, 
                       s2.Length+1),s2 + @"\")==0)  
                nodeNNode = new TreeNode();
                nodeNNode.Tag =  s1node.Substring(0,s1node.Length -6);
                nodeNNode.Text = 
                 s1node.Substring(s2.Length+1,s1node.Length -s2.Length-7) ;
                nodeNNode.ImageIndex = 28;
                nodeNNode.SelectedImageIndex = 28;


Enumerate network servers

This function basically creates the tree nodes of the network places, and decides which all network resources are the domains and which all the shared folders under that domain.

private void EnumerateServers(NETRESOURCE pRsrc, 
     ResourceScope scope, ResourceType type, 
     ResourceUsage usage, ResourceDisplayType displayType, 
     string kPath)
uint        bufferSize = 16384;
IntPtr        buffer    = Marshal.AllocHGlobal((int) bufferSize);
IntPtr        handle = IntPtr.Zero;
ErrorCodes    result;
uint        cEntries = 1;
bool serverenum = false;

result = WNetOpenEnum(scope, type, usage, pRsrc, out handle);

if (result == ErrorCodes.NO_ERROR)
        result = WNetEnumResource(handle, ref cEntries, buffer, 
                                  ref bufferSize);

        if ((result == ErrorCodes.NO_ERROR))
            Marshal.PtrToStructure(buffer, pRsrc);

  if ((pRsrc.dwDisplayType==displayType) || 
      (pRsrc.dwDisplayType == ResourceDisplayType.RESOURCEDISPLAYTYPE_DOMAIN))
  aData.Add(pRsrc.lpRemoteName + "-" + pRsrc.dwDisplayType );

 if ((pRsrc.dwUsage&ResourceUsage.RESOURCEUSAGE_CONTAINER ) == 
                    ResourceUsage.RESOURCEUSAGE_CONTAINER )
       if ((pRsrc.dwDisplayType== displayType))
           EnumerateServers(pRsrc, scope, type, usage, displayType,kPath);
        if (pRsrc.dwDisplayType    == displayType)
            EnumerateServers(pRsrc, scope,type, usage, displayType,kPath);
            serverenum = true;
        if (!serverenum)
            if (pRsrc.dwDisplayType == 
                aData.Add(pRsrc.lpRemoteName + "-share");
            serverenum =false;
     "Microsoft Windows Network")==0))
            EnumerateServers(pRsrc, scope, type, usage, displayType,kPath);

        else if    (result    != ErrorCodes.ERROR_NO_MORE_ITEMS)
    } while    (result    != ErrorCodes.ERROR_NO_MORE_ITEMS);


Marshal.FreeHGlobal((IntPtr) buffer);

User Interface

About the user interface how to give it a professional touch. First thing is to create an icon for the control which will be displayed in the toolbox as shown below

Add a 16 x 16 gif image tree.gif here in the project and change its property BUILD ACTION = Embedded Resource and add the following

          "tree.gif"),DefaultEvent("PathChanged")    ] 

public class ExplorerTree : System.Windows.Forms.UserControl


Any user control created in .Net is reusable in the sense that , once you compile it as a dll file , you can add it in the toolbox as shown in the figure below , and that control can be used with VisualBasic.Net or C# or any other .net interoperable language

You can create as many instances of the user control as shown below
4 ExplorerTree control with different properties, can you SPOT the difference


Now the most important part, How to extend our user control , one way which i thought would be useful is to add shortcut to frequently used folders, so you don't have to go through the whole tree structure. the most common example is the way "My Documents" is handled by Windows Explorer, though "My documents" actually resides in "C:\Documents and Settings\Username\My Documents" but the windows explorer gives us a shortcut to go there directly. So keeping this in mind , i have extended the ExplorerTree so that user can add/remove shortcuts to frequently used folders as shown below

The Code behind : Add shortcuts for frequently used folders

private void AddFolderNode(string name, string path)

        TreeNode nodemyC = new TreeNode();
        nodemyC.Tag = path;
        nodemyC.Text = name;

        nodemyC.ImageIndex = 18;
        nodemyC.SelectedImageIndex = 18;


            //add dirs under drive
            if (Directory.Exists (path))
                foreach(string dir in Directory.GetDirectories(path))
                    TreeNode node = new TreeNode();
                    node.Tag = dir;
                    node.Text = dir.Substring(dir.LastIndexOf(@"\") + 1);
                    node.ImageIndex = 1;
        catch(Exception ex)    //error just add blank dir
            MessageBox.Show ("Error while Filling the Explorer:" + 
                             ex.Message );
    catch(Exception e)
        MessageBox.Show (e.Message);  

In Action: ExplorerTree control in a Test application

With Visual Basic FileListBox


Thanks to Rob Manderson for his article on porting the NetResource class in C sharp here and also all those authors who have contributed for code project whose ideas knowingly or unknowingly, directly or indirectly have influenced me to write this article.

Article History

</a />
  • June 25, 2006: First published.
  • July 20, 2006: Bug fixed as mentioned by Lyle M, thanks
  • August 11, 2006: Bug fixes mentioned by Doncp and pcxp, thanks
  • August 11, 2006: Features added for customizable interface, without toolbar amd addressbar
  • Dec 14, 2006: Fixed and updated with all the requested features (see comments below) thanks to psxp, evan stein and otopia and BigfootIndy2k6 for there comments
  • Mar 15, 2007: Fixed the bug mentioned by scorpion_pgm82

And thanks

For coming so far! I hope you find this useful, and give me a vote/comment if you do and take care.


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

You may also be interested in...


Comments and Discussions

SuggestionGreat application but, Pin
Member 1177070017-Jun-15 5:03
memberMember 1177070017-Jun-15 5:03 
QuestionVote Pin
d.amann31-Jan-14 18:45
memberd.amann31-Jan-14 18:45 
Bugbug Pin
respirator11-Jan-14 13:22
memberrespirator11-Jan-14 13:22 
GeneralMy vote of 5 Pin
Member 81005074-Mar-13 12:18
memberMember 81005074-Mar-13 12:18 
GeneralMy vote of 5 Pin
Supriya Srivastav12-Feb-13 21:33
memberSupriya Srivastav12-Feb-13 21:33 
SuggestionYou can also get actual Windows Explorer by pointing WebBrowser control at C:\ Pin
Dwedit_31-Aug-12 5:36
memberDwedit_31-Aug-12 5:36 
ganeshbarli15-Jul-12 19:44
memberganeshbarli15-Jul-12 19:44 
GeneralMy vote of 5 Pin
Matt Esterak29-Nov-11 13:32
memberMatt Esterak29-Nov-11 13:32 
GeneralMy vote of 5 Pin
SercanOzdemir7-Nov-11 7:25
memberSercanOzdemir7-Nov-11 7:25 
QuestionNice Pin
ghvnd15-Oct-11 20:49
memberghvnd15-Oct-11 20:49 
AnswerRe: Nice Pin
Member 99566577-Feb-15 11:42
memberMember 99566577-Feb-15 11:42 
GeneralMy vote of 1 Pin
Syed Javed9-Nov-10 3:35
memberSyed Javed9-Nov-10 3:35 
GeneralUser Control in Asp.Net Pin
malempatich3-Jun-10 9:39
membermalempatich3-Jun-10 9:39 
GeneralFTP Pin
jammmie99931-May-09 0:00
memberjammmie99931-May-09 0:00 
GeneralManually typed directory gives error Pin
jm3328-May-08 12:23
memberjm3328-May-08 12:23 
GeneralMy Network Places not working in Vista and partially in XP, fully only under Win2000[modified] Pin
ionutzu4-May-08 0:51
memberionutzu4-May-08 0:51 
GeneralWindows Explorer is now a part of FREE software ToeTag Pin
Quartz.2-Mar-08 15:43
mvpQuartz.2-Mar-08 15:43 
QuestionFolders don't update correctly if the files are changed outside of the explorer tree.. Pin
flacoman9117-Nov-07 17:22
memberflacoman9117-Nov-07 17:22 
GeneralRe: Folders don't update correctly if the files are changed outside of the explorer tree.. Pin
poirelotto13-Feb-08 3:26
memberpoirelotto13-Feb-08 3:26 
GeneralRe: Folders don't update correctly if the files are changed outside of the explorer tree.. Pin
flacoman9113-Feb-08 3:54
memberflacoman9113-Feb-08 3:54 
Questionany thoughts on adding a file system watcher? Pin
flacoman9110-Nov-07 7:23
memberflacoman9110-Nov-07 7:23 
Generalfiring an event on a form hosting the control. [modified] Pin
Greg Hazzard29-Mar-07 6:57
memberGreg Hazzard29-Mar-07 6:57 
GeneralPersisting Shortcuts Pin
yesildal15-Mar-07 8:42
memberyesildal15-Mar-07 8:42 
GeneralFolders SHown Twice on Desktop Pin
scorpion_pgm827-Mar-07 22:56
memberscorpion_pgm827-Mar-07 22:56 
AnswerRe: Folders SHown Twice on Desktop Pin
Quartz...8-Mar-07 7:11
memberQuartz...8-Mar-07 7:11 
GeneralRe: Folders SHown Twice on Desktop Pin
scorpion_pgm8214-Mar-07 19:26
memberscorpion_pgm8214-Mar-07 19:26 
AnswerRe: Folders SHown Twice on Desktop Pin
Quartz...15-Mar-07 7:43
memberQuartz...15-Mar-07 7:43 
GeneralRe: Folders SHown Twice on Desktop Pin
scorpion_pgm8215-Mar-07 23:54
memberscorpion_pgm8215-Mar-07 23:54 
GeneralRe: Folders SHown Twice on Desktop Pin
scorpion_pgm8229-Mar-07 22:52
memberscorpion_pgm8229-Mar-07 22:52 
GeneralRe: Folders SHown Twice on Desktop Pin
Abei Vittorio15-Aug-14 17:42
memberAbei Vittorio15-Aug-14 17:42 
Questionlaunch into ExplorerTree from a most recent directories list? Pin
Greg Hazzard14-Feb-07 8:08
memberGreg Hazzard14-Feb-07 8:08 
AnswerRe: launch into ExplorerTree from a most recent directories list? Pin
Quartz...14-Feb-07 8:38
memberQuartz...14-Feb-07 8:38 
GeneralRe: launch into ExplorerTree from a most recent directories list? [modified] Pin
Greg Hazzard14-Feb-07 13:27
memberGreg Hazzard14-Feb-07 13:27 
QuestionShellCommands Pin
hootsman29-Jan-07 2:42
memberhootsman29-Jan-07 2:42 
AnswerRe: ShellCommands Pin
Quartz...29-Jan-07 8:28
memberQuartz...29-Jan-07 8:28 
QuestionUpdated version? Pin
psxp13-Dec-06 5:50
memberpsxp13-Dec-06 5:50 
AnswerRe: Updated version? - FIXED V 2.0 Pin
Quartz...14-Dec-06 12:47
memberQuartz...14-Dec-06 12:47 
GeneralUpgrade for the GetDirectories() function Pin
Evan Stein16-Sep-06 23:58
memberEvan Stein16-Sep-06 23:58 
GeneralRe: Upgrade for the GetDirectories() function Pin
Quartz...17-Sep-06 16:50
memberQuartz...17-Sep-06 16:50 
AnswerRe: Upgrade for the GetDirectories() function - FIXED V 2.0 Pin
Quartz...14-Dec-06 12:48
memberQuartz...14-Dec-06 12:48 
Generalbug - btnUp moves to wrong parent Pin
optopia12-Sep-06 13:02
memberoptopia12-Sep-06 13:02 
GeneralRe: bug - btnUp moves to wrong parent Pin
optopia12-Sep-06 13:11
memberoptopia12-Sep-06 13:11 
GeneralRe: bug - btnUp moves to wrong parent Pin
Quartz...14-Sep-06 8:50
memberQuartz...14-Sep-06 8:50 
AnswerRe: bug - btnUp moves to wrong parent - FIXED V 2.0 Pin
Quartz...14-Dec-06 12:49
memberQuartz...14-Dec-06 12:49 
GeneralGreat Control Pin
slicht9-Sep-06 19:51
memberslicht9-Sep-06 19:51 
GeneralRe: Great Control Pin
Quartz...9-Sep-06 22:19
memberQuartz...9-Sep-06 22:19 
GeneralBug fix for Null Exception Pin
psxp8-Aug-06 12:36
memberpsxp8-Aug-06 12:36 
GeneralRe: Bug fix for Null Exception Pin
Quartz...10-Aug-06 12:46
memberQuartz...10-Aug-06 12:46 
GeneralRe: Bug fix for Null Exception Pin
psxp10-Aug-06 15:40
memberpsxp10-Aug-06 15:40 
AnswerRe: Bug fix for Null Exception Pin
Quartz...11-Aug-06 14:11
memberQuartz...11-Aug-06 14:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 15 Mar 2007
Article Copyright 2006 by Raj Lal
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid