Click here to Skip to main content
15,884,836 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 
Hi again,

I just found a bug:

when doing "Get Full Path" and the selected node is the root node, then you will get a "System.NullReferenceException" because this node doesn't have a parent node. The problem is here:

public string GetFullName()
{
TreeNode walkingNode = treeSourceFiles.SelectedNode;
string retVal = walkingNode.Text;

//This while will loop twice when the root node is
//the selected one. The root node doesn't have a
//parent, so, null will be returned the second time
//and passed to the GetTag method, here you will try
//to do "null.Tag.ToString().Substring(0,3)", which
//off course will fail
while (GetTag(walkingNode)!=TAG_DOMAIN)
{
retVal = walkingNode.Text + "\\" + retVal;
//Ops the root node doesn't have a parent, so,
//null will be returned
walkingNode = walkingNode.Parent;
}
return("\\\\" + retVal);
}

Wouldn't be better if you return "\\", the null string "" or null? You can correct the problem with this:

public string GetFullName()
{
TreeNode walkingNode = treeSourceFiles.SelectedNode;
string retVal = "";
if (GetTag(walkingNode)!=TAG_ROOTNODE)
{
retVal = walkingNode.Text;
while (GetTag(walkingNode)!=TAG_DOMAIN)
{
retVal = walkingNode.Text + "\\" + retVal;
walkingNode = walkingNode.Parent;
}
}
else
return "";
return("\\\\" + retVal);
}

Best regards,
Josef
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 
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.