How to browse local lan server?

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

LVL 2
yaapAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.