We help IT Professionals succeed at work.

How to browse local lan server?

yaap
yaap asked
on
Medium Priority
239 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

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.