?
Solved

How to browse local lan server?

Posted on 2006-03-20
3
Medium Priority
?
223 Views
Last Modified: 2010-04-16
Hi

I want to browse recursively local lan server resources.
I have only server name without shares list.

Eg: \\john\

When I used DirectoryInfo I received the following exception:
"The UNC path should be of the form \\\\server\share".

--
Regards

0
Comment
Question by:yaap
1 Comment
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 16237952
Here is a class that uses WMI to get share names for a machine:

using System;
using System.Collections;
using System.Management;

public enum ShareType : long
{
  DiskDrive = 0,
  PrintQueue = 1,
  Device = 2,
  Ipc = 3,
  DiskDriveAdmin = 0x80000000,
  PrintQueueAdmin = 0x80000001,
  DeviceAdmin = 0x80000002,
  IpcAdmin = 0x80000003
}
 
public class Win32_Share
{
  public Int32 AccessMask;
  public bool AllowMaximum;
  public string Description;
  public Int32 MaximumAllowed;
  public string Name;
  public string Path;
  public string Status;
  public ShareType Type;

  /// <summary>
  /// Overloaded method to convert a computer share name
  /// to the full path on the computer.   This form is for
  /// access to a remote machine.  The arguments are the
  /// machine name, user name + password for a user with
  /// rights to the remote machine.
  /// </summary>
  /// <param name="machine">Machine name with or without leading \\</param>
  /// <param name="user">User name with access rights to machine</param>
  /// <param name="password">Password for the user</param>
  /// <param name="shareName">The share name to convert</param>
  /// <returns>String indicating full path on the machine</returns>
  public static string ShareNameToPath(string machine, string user, string password, string shareName)
  {

    ManagementScope scope = CreateScope(machine, user, password);

    return ShareNameToPath(scope, shareName);

  }


  /// <summary>
  /// Create a scope to access a remote machine
  /// </summary>
  /// <param name="machine">The machine name</param>
  /// <param name="user">The user name with access rights</param>
  /// <param name="password">The password for the user</param>
  /// <returns>A ManagementScope object representing the correct options to connect</returns>
  private static ManagementScope CreateScope(string machine, string user, string password)
  {

    ManagementPath path = new ManagementPath();
    path.Server = machine.TrimStart(@"\".ToCharArray()) + @"\root\cimv2";

    ManagementScope scope = new ManagementScope(path);

    if (user.Length > 0)
    {
      ConnectionOptions options = new ConnectionOptions();
      options.Username = user;
      options.Password = password;
      scope.Options = options;
    }

    return scope;

  }


  /// <summary>
  /// Overloaded method to convert a computer share name
  /// to the full path on the computer.   This form is for
  /// access to a remote machine.  The arguments are the
  /// just machine name, assuming that the current user
  /// has access rights to the remote machine.
  /// </summary>
  /// <param name="machine">Machine name with or without leading \\</param>
  /// <param name="shareName">The share name to convert</param>
  /// <returns>String indicating full path on the machine</returns>
  public static string ShareNameToPath(string machine, string shareName)
  {

    ManagementPath path = new ManagementPath();
    path.Server = machine.TrimStart(@"\".ToCharArray()) + @"\root\cimv2";

    ManagementScope scope = new ManagementScope(path);

    return ShareNameToPath(scope, shareName);

  }

  /// <summary>
  /// Overloaded method to convert a computer share name
  /// to the full path on the computer.   This form is for
  /// access to the local machine.  The arguments are the
  /// just the share name--the scope is passed as null to
  /// indicate local machine.
  /// </summary>
  /// <param name="shareName">The share name to convert</param>
  /// <returns>String indicating full path on the machine</returns>
  public static string ShareNameToPath(string shareName)
  {
    ManagementScope nil = null;
    return ShareNameToPath(nil, shareName);
  }

  /// <summary>
  /// Overloaded method to convert a computer share name
  /// to the full path on the computer.   This private
  /// method is the one that does all the work for the
  /// other methods.
  /// </summary>
  /// <param name="scope">The scope indicates path to the remote machine and user name + password</param>
  /// <param name="shareName">The share name to convert</param>
  /// <returns>String indicating full path on the machine</returns>
  private static string ShareNameToPath(ManagementScope scope, string shareName)
  {
    ManagementObject shareObject = new ManagementObject(string.Format("Win32_Share.Name='{0}'", shareName));
    shareObject.Get();

    if (scope != null)
      shareObject.Scope = scope;

    return (string)shareObject.Properties["Path"].Value;
  }

  /// <summary>
  /// Return a list of Win32_Share objects that convert the
  /// late-bound WMI objects to a type-specific class object
  /// for easy access (Intellisense) to the properties.
  /// </summary>
  /// <returns></returns>
  public static Win32_Share[] GetList(string machine, string user, string password)
  {
    ManagementScope scope = CreateScope(machine, user, password);
    return GetList(scope);
  }
   
  /// <summary>
  /// Return a list of Win32_Share objects that convert the
  /// late-bound WMI objects to a type-specific class object
  /// for easy access (Intellisense) to the properties.
  /// </summary>
  /// <returns></returns>
  public static Win32_Share[] GetList(string machine)
  {
    ManagementScope scope = CreateScope(machine, "", "");
    return GetList(scope);
  }
   
  /// <summary>
  /// Return a list of Win32_Share objects that convert the
  /// late-bound WMI objects to a type-specific class object
  /// for easy access (Intellisense) to the properties.
  /// </summary>
  /// <returns></returns>
  public static Win32_Share[] GetList(ManagementScope scope)
  {
    string query = "Select * From Win32_Share";
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
    ArrayList listShares = new ArrayList();
    foreach (ManagementObject entryCurrent in searcher.Get())
    {
      Win32_Share share = new Win32_Share();
      share.AccessMask = Convert.ToInt32(entryCurrent["AccessMask"]);
      share.AllowMaximum = (bool)entryCurrent["AllowMaximum"];
      share.Description = (string)entryCurrent["Description"];
      share.MaximumAllowed = Convert.ToInt32(entryCurrent["MaximumAllowed"]);
      share.Name = (string)entryCurrent["Name"];
      share.Path = (string)entryCurrent["Path"];
      share.Status = (string)entryCurrent["Status"];
      share.Type = (ShareType)Convert.ToInt64(entryCurrent["Type"]);
      listShares.Add(share);
    }
    return ((Win32_Share[])(listShares.ToArray(typeof(Win32_Share))));
  }
}

Sample usage:

   Win32_Share[] shares = Win32_Share.GetList("john");

Bob
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question