Click here to Skip to main content
15,884,986 members
Articles / Programming Languages / C#
Article

A control class for browsing through the Network Neighborhood

Rate me:
Please Sign up or sign in to vote.
4.32/5 (11 votes)
3 Sep 2004CPOL2 min read 74.7K   1.3K   52   10
If you want to have an easy-to-use Dialog Box to browse through the network, have a look at this. This library allows for selecting network shares, directories, files, ... and will ask for a user name and password, if needed.

Sample Image - ScreenShot1.jpg

Introduction

NetworkSelect is a control library that allows you to browse through the Network Neighborhood. It is possible to select the level of download, from domains, down to shares, directories, and files. The control will ask for a user name and password if a computer cannot be accessed.

Credits

This control is based on the excellent work of Richard Deeming (CodeProject member ID:34187) and Marc Merritt (member ID: 2851). They both provided some means to access network shares, and I've included (parts of) their classes in this project. Despite their good work, I still did not have some user-friendly way to browse through the Network Neighborhood, and that's how this control got created.

Using the code

The control code spreads a larger number of pages, so allow me not to go too deep into the details of the class itself.

In its simplest form, the control is used pretty straightforward.

  • Step 1 Expand the NetworkSelect.zip file.
  • Step 2 Create a new C# project in the same folder.
  • Step 3 During development, you'd probably like to copy the images folder to your bin\Debug and bin\Release folders. The application expects that this images folder be present in the current application directory.
  • Step 4 When opening the new project, add the following "Existing files" to your project:
    • AskPassword.cs
    • CompEnum.cs
    • NetworkSelect.cs
    • Shares.cs

    Then perform a "Build Solution" to add the control to your project.

  • Step 5 Draw the NetworkSelect control in the Form1. Add a Button, button1, to the form.
  • Step 6 Add the following using clause in the header of your program:
    C#
    using TIData.NetworkSelect;
  • Step 6 Add the following button1_Click method to your program:
    C#
    private void button1_Click(object sender, System.EventArgs e)
    {
      networkSelect1.SeekThroughDomains(
        NetworkSelect.HIDE_CHECKBOXES,
        NetworkSelect.SHOW_SHARES,
        NetworkSelect.SHOW_HIDDENSHARES,
        NetworkSelect.SHOW_DIRECTORIES,
        NetworkSelect.SHOW_FILES);
    }
  • Step 7 The domains, computers, shares, ..., files can be extracted using the following methods:

    C#
    string myDomain = networkSelect1.GetCurrentDomain();
    string myComputer = networkSelect1.GetCurrentComputer();
    string myShare = networkSelect1.GetCurrentShare();
    string myDir = networkSelect1.GetCurrentDirectory();
    string myFile = networkSelect1.GetCurrentFile();
    string fullPath = networkSelect1.GetFullName();

Finally, all the rest is for you...

A few notes

Regrettably, browsing through the Network can be a time consuming task. This is certainly the case whenever you don't have full rights to all domain computers. Therefore, the control shows a "Working hard for you, please wait..." message when enumerating domains, servers, ... files.

Although you can enable check boxes, I haven't included a method to read the checked paths. Hey, this is the very first release: 0.90. Shouldn't take too much time. Leave me a note if you badly need it.

I already know that the control shows up with a few Warnings too much. Will be fixed in the next release. The good thing is that it also shows a pop-up asking for a username/password whenever you don't manage to view the shares of a computer.

Version History

Not much of a history. This is the first release. Let's call it V0.9.0.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Belgium Belgium
Ever occupied IT consultant.

Comments and Discussions

 
Questioncreate a shared folder? Pin
nimo114-Dec-05 23:16
nimo114-Dec-05 23:16 
GeneralBug when getting the Full path Pin
Josef Meile22-Mar-05 2:18
Josef Meile22-Mar-05 2:18 
GeneralRe: Bug when getting the Full path 2 Pin
Josef Meile22-Mar-05 9:06
Josef Meile22-Mar-05 9:06 
GeneralCongratulations and a few improvements Pin
Josef Meile11-Mar-05 8:19
Josef Meile11-Mar-05 8:19 
Hi,

first at all, I want to congratulate and thank you for this nice code. It
works really good. Second, I did some modifications to the code in order to
make it more efficient in terms of speed.

As you said, network browsing can be time consuming. However, you could reduce
that time if:

1) You only create the subnodes of a component when the user double clicks on
it.

2) You don't recreate the subnodes each time that you expand the parent node.
If you want to do this, you will have to do it manually.

Basically, I did the following:

1) At first, I only expand the root node, so that only the domains are
visible. That's what windows does when browsing the network by double
clicking on the "My Network Places" icon.

2) Next, if the user double clicks on a domain, then the computers will be
displayed, but the program won't try to figure out their shares. The
advantage of this is that you won't be asked several times for a user and
a password if you don't have full rights over the network.

3) If the user double clicks or collapses and expands that domain again, the
subnodes won't be recreated. In order to do this, s/he will have to choose
the refresh option on a context menu or s/he will have to click on a button.
This is really useful because sometimes you have lots of pc's under the same
domain, so, it will be expensive to query the network again and usually you
don't want to look at every computer on the network. On the other hand, if,
let's say, you lost your network connection, once you recover it, you can
refresh the domain node contents.

3) Finally, the same behaviour will be followed for the computers. You have to
double click on them to see their shares.

Now the directions:

1) Add a context menu to the NetSelect form. Called it mnuTreeContext for
example.

2) Add a menu item to that context menu. Called it mnuRefresh for example.

3) Add a Click handler for the menu item and put the following code there:

private void mnuRefresh_Click(object sender, System.EventArgs e)
{
RefreshNodes();
}

4) Add the following method to the NetSelect class:

public void RefreshNodes()
{
TreeNode currentNode = treeSourceFiles.SelectedNode;
if (currentNode != null)
{
currentNode.Nodes.Clear();
GoDownTheDrain(currentNode); // Go get all subdivisions
currentNode.Expand();
treeSourceFiles.SelectedNode = currentNode;
}
}

5) Add the following double click handler to the treeSourcesFiles component:

private void treeSourceFiles_DoubleClick(object sender, System.EventArgs e)
{
if (treeSourceFiles.SelectedNode.Nodes.Count == 0)
RefreshNodes();
}
}

6) Change the treeSourceFiles_AfterExpand handler by this one:

private void treeSourceFiles_AfterExpand(object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
TreeNode currentNode;
string currentTag;

currentTag = GetTag(treeSourceFiles.SelectedNode);

//I don't want to automatically expand Computer or domains.
if ((currentTag != TAG_COMPUTER) && (currentTag != TAG_SERVER) &&
(currentTag != TAG_DOMAIN))
{
// set the first directory below our current location
treeSourceFiles.SelectedNode = e.Node.FirstNode;
while(treeSourceFiles.SelectedNode != null)
{
currentNode = treeSourceFiles.SelectedNode;
currentTag = GetTag(currentNode);
SelectProperIcon(treeSourceFiles.SelectedNode,ACTION_EXPAND);

//I add this condition to avoid automatically expanding domains
//and computer. Additionally, if a node already has subnodes,
//they won't be recreated. The user has to refresh them
if ((currentTag != TAG_COMPUTER) && (currentTag != TAG_SERVER) &&
(currentTag != TAG_DOMAIN) && (currentNode.Nodes.Count == 0))
GoDownTheDrain(treeSourceFiles.SelectedNode);
treeSourceFiles.SelectedNode=currentNode.NextNode;
}
}
treeSourceFiles.SelectedNode = e.Node;
}

The following steps are optional:

7) Add a refresh button to the form where you defined your NetworkSelect
object and called it btnRefresh. Let's also say that your NetworkSelect
object is called netSelect.

8) Add the following Click handler for your button:

private void btnRefresh_Click(object sender, System.EventArgs e)
{
netSelect.RefreshNodes();
}

To do:

If you are planning to use the demo project, you could also add a
checkbox to automatically refresh the network domains and computers whenever
they are first created, or the user expands or double clicks them.

If you find a bug, I will be gratefull if you write me an email.

Thanks,
Josef
GeneralRe: Congratulations and a few improvements Pin
Josef Meile11-Mar-05 8:30
Josef Meile11-Mar-05 8:30 
GeneralRe: Congratulations and a few improvements Pin
Josef Meile11-Mar-05 10:29
Josef Meile11-Mar-05 10:29 
GeneralC++ version Pin
1of34-Mar-05 6:02
1of34-Mar-05 6:02 
GeneralRe: C++ version Pin
JurgenVanGorp4-Mar-05 7:06
JurgenVanGorp4-Mar-05 7:06 
GeneralChecked Items Pin
girlatus25-Oct-04 11:54
sussgirlatus25-Oct-04 11:54 
Generalnice work go on Pin
Taha Elsayed24-Sep-04 15:25
Taha Elsayed24-Sep-04 15:25 

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.