Question : C# & WMI get mapped shares

I am looking at writing a C# application that will get a list of mapped drives for another computer on the network.   I assume I will be using WMI, but I am open to other options as well, but it must scan computers remotely, no software can be placed on the other computers.

This program will be run by a member of "Enterprise Admins" and "Domain Admins".  I already have a list of computers, I just need to get the list of mapped shares when I have a specific computer name.

.NET 2.0, 3.5, and 4.0 are all available.

Answer : C# & WMI get mapped shares

check attached code snippet.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
static 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 + @"\root\cimv2");

                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)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
Random Solutions  
 
programming4us programming4us