Click here to Skip to main content
16,016,229 members
Articles / Programming Languages / C#
Article

A Windows Explorer in a user control

Rate me:
Please Sign up or sign in to vote.
4.85/5 (84 votes)
15 Mar 20075 min read 463.4K   20.4K   251   92
Functionality of Windows Explorer in a user control using C#

Sample Image - ExplorerTree.jpg

Introduction

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.

Image 2

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.

THE UPDATE

Customizable Toolbar and addressbar

updated

Functionality

Image 4

Features

  • 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

Properties

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

Image 5

Events

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

The Code behind : explore my computer

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

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

    nodeDrive.Text = drive ;

    //Determine icon to display by drive
    switch(Win32.GetDriveType(drive))
    {
        case 2:
            nodeDrive.ImageIndex = 17;
            nodeDrive.SelectedImageIndex  = 17;
            break;
        case 3:
            nodeDrive.ImageIndex = 0;
            nodeDrive.SelectedImageIndex  = 0;
            break;
        case 4:
            nodeDrive.ImageIndex = 8;
            nodeDrive.SelectedImageIndex = 8;
            break;
        case 5:
            nodeDrive.ImageIndex = 7;
            nodeDrive.SelectedImageIndex = 7;
            break;
        default:
            nodeDrive.ImageIndex = 0;
            nodeDrive.SelectedImageIndex = 0;
            break;
    }

    nodemyC.Nodes.Add(nodeDrive);
    nodeDrive.EnsureVisible();
    tvwMain.Refresh(); 
    try
    {
        //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;
                nodeDrive.Nodes.Add(node);
            }
        }

    }
    catch(Exception)    //error just add blank dir
    {
    }
    nodemyC.Expand(); 
}

Image 6

The Code behind : explore my folders

C#
if (ShowMyDocuments)
{
    nodemd = new TreeNode();
    nodemd.Tag = Environment.GetFolderPath(
                 Environment.SpecialFolder.Personal);
    nodemd.Text = "My Documents";
    nodemd.ImageIndex = 9;
    nodemd.SelectedImageIndex = 9;
    nodeD.Nodes.Add(nodemd);
    FillFilesandDirs(nodemd);
}

if (ShowMyNetwork)
{
    nodemyN = new TreeNode();
    nodemyN.Tag = "My Network Places";
    nodemyN.Text = "My Network Places";
    nodemyN.ImageIndex = 13;
    nodemyN.SelectedImageIndex = 13;
    nodeD.Nodes.Add(nodemyN);
    nodemyN.EnsureVisible();

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

    nodeNN = new TreeNode();
    nodeNN.Tag = "Network Node";
    nodeNN.Text = "Network Node";
    nodeNN.ImageIndex = 15;
    nodeNN.SelectedImageIndex = 15;
    nodeEN.Nodes.Add(nodeNN);
    nodeEN.EnsureVisible();
}

if (ShowMyFavorites)
{
    nodemf = new TreeNode();
    nodemf.Tag = Environment.GetFolderPath(
                 Environment.SpecialFolder.Favorites);
    nodemf.Text = "My Favorites";
    nodemf.ImageIndex = 26;
    nodemf.SelectedImageIndex = 26;
    nodeD.Nodes.Add(nodemf);
    FillFilesandDirs(nodemf);
}

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.

Image 7

The Code behind : Go Back and Go Forward

C#
private void UpdateListGoBack() 
{    
 if ((listView1.Items.Count >0)&&
     (String.Compare(listView1.Items[0].SubItems[1].Text,"Selected")==0))
        return;
     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) && 
        (String.Compare(
         listView1.Items[listView1.Items.Count -1 ].SubItems[1].Text, 
         "Selected")==0))
        return;
    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

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

    int icount =0;
    UpdateListAddCurrent();
    icount = listView1.Items.Count + 1;
    try
    {
        if (listView1.Items.Count> 0)
        {    
            if (String.Compare(listView1.Items[listView1.Items.Count-1].Text, f)==0)
            {
                return;
            }
        }
    
        for (i = 0;i < listView1.Items.Count;i++)
        {
            listView1.Items[i].SubItems[1].Text = " -/- ";
        }
        listviewitem = new ListViewItem(f);
        listviewitem.SubItems.Add("Selected");
        listviewitem.Tag = f;
        this.listView1.Items.Add(listviewitem);
    }
    catch(Exception e)
    {
    MessageBox.Show(e.Message);   
    }
}

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.

C#
ServerEnum servers = new ServerEnum(ResourceScope.RESOURCE_GLOBALNET,
    ResourceType.RESOURCETYPE_DISK, 
    ResourceUsage.RESOURCEUSAGE_ALL, 
    ResourceDisplayType.RESOURCEDISPLAYTYPE_SERVER,pS);


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;
        n.Nodes.Add(nodeNN);
        foreach    (string    s1node in servers)
        {
            if (s1node.Length >6)
            {
                if(String.Compare(s1node.Substring(s1node.Length-6,6),"-share")==0)
                {
                    if (s2.Length <=s1node.Length )
                    {

        try
            {
            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;
                nodeNN.Nodes.Add(nodeNNode);
                }
            }
            catch(Exception)
            {}
        }
        }
        }
    }
    }

}

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.

C#
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)
{
    do
    {
        result = WNetEnumResource(handle, ref cEntries, buffer, 
                                  ref bufferSize);

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

 if(String.Compare(kPath,"")==0)
  {
  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);
           
          }
               
        }
    }
    else
    {
        if (pRsrc.dwDisplayType    == displayType)
        {
            aData.Add(pRsrc.lpRemoteName);
            EnumerateServers(pRsrc, scope,type, usage, displayType,kPath);
            //return;
            serverenum = true;
        }
        if (!serverenum)
        {
            if (pRsrc.dwDisplayType == 
                  ResourceDisplayType.RESOURCEDISPLAYTYPE_SHARE)
            {
                aData.Add(pRsrc.lpRemoteName + "-share");
            }
        }
        else
        {
            serverenum =false;
        }
 if((kPath.IndexOf(pRsrc.lpRemoteName)>=0)||
    (String.Compare(pRsrc.lpRemoteName, 
     "Microsoft Windows Network")==0))
        {
            EnumerateServers(pRsrc, scope, type, usage, displayType,kPath);
            //return;
            
        }

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

    WNetCloseEnum(handle);
}

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


Image 8

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

C#
[ToolboxBitmapAttribute(typeof(WindowsExplorer.ExplorerTree), 
          "tree.gif"),DefaultEvent("PathChanged")    ] 

public class ExplorerTree : System.Windows.Forms.UserControl
{

Reusability

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

Image 9

Extensibility

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

Image 10

The Code behind : Add shortcuts for frequently used folders

C#
private void AddFolderNode(string name, string path)
{

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

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

        TreeNodeRootNode.Nodes.Add(nodemyC); 

        try
        {
            //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;
                    nodemyC.Nodes.Add(node);
                }
            }
            
            
        }
        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

Image 11

Acknowledgement

Thanks to Rob Manderson for his article on porting the NetResource class in C sharp here http://www.codeproject.com/csharp/csenumnetworkresources.asp 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

  • 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.

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


Written By
Founder Teamcal AI
United States United States

Comments and Discussions

 
QuestionNever Expands Entire Network Pin
Member 1089056722-Jan-22 19:34
Member 1089056722-Jan-22 19:34 
QuestionRe: Never Expands Entire Network Pin
CalWho31-Jan-22 20:27
CalWho31-Jan-22 20:27 
SuggestionGreat application but, Pin
Member 1177070017-Jun-15 5:03
Member 1177070017-Jun-15 5:03 
QuestionVote Pin
d.amann31-Jan-14 18:45
d.amann31-Jan-14 18:45 
Bugbug Pin
respirator11-Jan-14 13:22
respirator11-Jan-14 13:22 
GeneralMy vote of 5 Pin
Member 81005074-Mar-13 12:18
Member 81005074-Mar-13 12:18 
GeneralMy vote of 5 Pin
Supriya Srivastav12-Feb-13 21:33
Supriya 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
Dwedit_31-Aug-12 5:36 
QuestionNETWORK EXPLORER IN TREE VIEW MODE. Pin
ganeshbarli15-Jul-12 19:44
ganeshbarli15-Jul-12 19:44 
GeneralMy vote of 5 Pin
Matt Esterak29-Nov-11 13:32
Matt Esterak29-Nov-11 13:32 
GeneralMy vote of 5 Pin
SercanOzdemir7-Nov-11 7:25
SercanOzdemir7-Nov-11 7:25 
QuestionNice Pin
ghvnd15-Oct-11 20:49
ghvnd15-Oct-11 20:49 
AnswerRe: Nice Pin
Member 99566577-Feb-15 11:42
Member 99566577-Feb-15 11:42 
GeneralMy vote of 1 Pin
Syed J Hashmi9-Nov-10 3:35
Syed J Hashmi9-Nov-10 3:35 
poor
GeneralUser Control in Asp.Net Pin
malempatich3-Jun-10 9:39
malempatich3-Jun-10 9:39 
GeneralFTP Pin
jammmie99931-May-09 0:00
professionaljammmie99931-May-09 0:00 
GeneralManually typed directory gives error Pin
jm3328-May-08 12:23
jm3328-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
ionutzu4-May-08 0:51 
GeneralWindows Explorer is now a part of FREE software ToeTag Pin
Raj Lal2-Mar-08 15:43
professionalRaj Lal2-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
flacoman9117-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
poirelotto13-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
flacoman9113-Feb-08 3:54 
Questionany thoughts on adding a file system watcher? Pin
flacoman9110-Nov-07 7:23
flacoman9110-Nov-07 7:23 
Generalfiring an event on a vb.net form hosting the control. [modified] Pin
Greg Hazz29-Mar-07 6:57
Greg Hazz29-Mar-07 6:57 
GeneralPersisting Shortcuts Pin
yesildal15-Mar-07 8:42
yesildal15-Mar-07 8:42 

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.