0 Replies - 246 Views - Last Post: 09 December 2009 - 07:19 PM

#1 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Retrieve list of mapped drives

Posted 09 December 2009 - 07:19 PM

Description: Need a reference to:

System.Collections.Generic
System.ManagementThis snippet uses WMI to retrieve a list of all mapped drives on the specified computer
/// <summary>
/// method for retrieving all mapped drives
/// </summary>
/// <returns></returns>
public List<string> GetMappedDriveList(string machine, string username, string pwd)
{
    try
    {
        List<string> drives = new List<string>();

        ConnectionOptions options = new ConnectionOptions();
        options.Username = username;
        options.Password = pwd;
        options.Impersonation = ImpersonationLevel.Impersonate;

        ManagementPath path = new ManagementPath(@"\" + machine + @"rootcimv2");

        ObjectQuery query = new ObjectQuery("select * from Win32_MappedLogicalDisk");

        ManagementScope scope = new ManagementScope(path, options);
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

        foreach (ManagementObject obj in searcher.Get())
            drives.Add(obj.Properties["Name"].Value.ToString());

        return drives;
    }
    catch (Exception ex)
    {
        MessageBox.Show(string.Format("Error retrieving mapped drives: {0}", ex.Message), "Mapped Drives", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return null;
    }
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1