I'm trying to find out how to programmatically (I'm using C#) determine the name (or i.p.) of servers to which my workstation has current maps. In other words, at some point in Windows Explorer I mapped a network drive to a drive letter (or used "net use w: " to map it). I know how to get the network drives on the system:
DriveInfo[] allDrives = DriveInfo.GetDrives(); foreach (DriveInfo d in allDrives) { if (d.IsReady && d.DriveType == DriveType.Network) { } }
But the DriveInfo class does not have properties that tell me what server and shared folder the mapped drive is associated with. Is there somewhere else I should be looking?
8 Replies. Start, click Start, then Computer button. All mapped drives are listed under Local Network. Double-click the mapped drive icon to see the files and folders to which you have access.
Open Command Prompt. Then type the command net share and hit Enter to continue. Then the shared folder will be listed. From the command line, you can also find the path of the shared folders.
On the command terminal, please then type the following: “net use”. 4 . Once this is entered, it will show you a full list of all the network drives mapped.
Click Start, Run, type cmd, and press Enter . At the MS-DOS prompt, type net share and press Enter . Each of the shares, the location of the resource, and any remarks for that share are displayed.
Have you tried to use WMI to do it?
using System; using System.Management; using System.Windows.Forms; public static void Main() { try { var searcher = new ManagementObjectSearcher( "root\\CIMV2", "SELECT * FROM Win32_MappedLogicalDisk"); foreach (ManagementObject queryObj in searcher.Get()) { Console.WriteLine("-----------------------------------"); Console.WriteLine("Win32_MappedLogicalDisk instance"); Console.WriteLine("-----------------------------------"); Console.WriteLine("Access: {0}", queryObj["Access"]); Console.WriteLine("Availability: {0}", queryObj["Availability"]); Console.WriteLine("BlockSize: {0}", queryObj["BlockSize"]); Console.WriteLine("Caption: {0}", queryObj["Caption"]); Console.WriteLine("Compressed: {0}", queryObj["Compressed"]); Console.WriteLine("ConfigManagerErrorCode: {0}", queryObj["ConfigManagerErrorCode"]); Console.WriteLine("ConfigManagerUserConfig: {0}", queryObj["ConfigManagerUserConfig"]); Console.WriteLine("CreationClassName: {0}", queryObj["CreationClassName"]); Console.WriteLine("Description: {0}", queryObj["Description"]); Console.WriteLine("DeviceID: {0}", queryObj["DeviceID"]); Console.WriteLine("ErrorCleared: {0}", queryObj["ErrorCleared"]); Console.WriteLine("ErrorDescription: {0}", queryObj["ErrorDescription"]); Console.WriteLine("ErrorMethodology: {0}", queryObj["ErrorMethodology"]); Console.WriteLine("FileSystem: {0}", queryObj["FileSystem"]); Console.WriteLine("FreeSpace: {0}", queryObj["FreeSpace"]); Console.WriteLine("InstallDate: {0}", queryObj["InstallDate"]); Console.WriteLine("LastErrorCode: {0}", queryObj["LastErrorCode"]); Console.WriteLine("MaximumComponentLength: {0}", queryObj["MaximumComponentLength"]); Console.WriteLine("Name: {0}", queryObj["Name"]); Console.WriteLine("NumberOfBlocks: {0}", queryObj["NumberOfBlocks"]); Console.WriteLine("PNPDeviceID: {0}", queryObj["PNPDeviceID"]); if(queryObj["PowerManagementCapabilities"] == null) Console.WriteLine("PowerManagementCapabilities: {0}", queryObj["PowerManagementCapabilities"]); else { UInt16[] arrPowerManagementCapabilities = (UInt16[])(queryObj["PowerManagementCapabilities"]); foreach (UInt16 arrValue in arrPowerManagementCapabilities) { Console.WriteLine("PowerManagementCapabilities: {0}", arrValue); } } Console.WriteLine("PowerManagementSupported: {0}", queryObj["PowerManagementSupported"]); Console.WriteLine("ProviderName: {0}", queryObj["ProviderName"]); Console.WriteLine("Purpose: {0}", queryObj["Purpose"]); Console.WriteLine("QuotasDisabled: {0}", queryObj["QuotasDisabled"]); Console.WriteLine("QuotasIncomplete: {0}", queryObj["QuotasIncomplete"]); Console.WriteLine("QuotasRebuilding: {0}", queryObj["QuotasRebuilding"]); Console.WriteLine("SessionID: {0}", queryObj["SessionID"]); Console.WriteLine("Size: {0}", queryObj["Size"]); Console.WriteLine("Status: {0}", queryObj["Status"]); Console.WriteLine("StatusInfo: {0}", queryObj["StatusInfo"]); Console.WriteLine("SupportsDiskQuotas: {0}", queryObj["SupportsDiskQuotas"]); Console.WriteLine("SupportsFileBasedCompression: {0}", queryObj["SupportsFileBasedCompression"]); Console.WriteLine("SystemCreationClassName: {0}", queryObj["SystemCreationClassName"]); Console.WriteLine("SystemName: {0}", queryObj["SystemName"]); Console.WriteLine("VolumeName: {0}", queryObj["VolumeName"]); Console.WriteLine("VolumeSerialNumber: {0}", queryObj["VolumeSerialNumber"]); } } catch (ManagementException ex) { MessageBox.Show("An error occurred while querying for WMI data: " + ex.Message); } }
to make it a little easier to get started download WMI Code Creater
You could use WMI to enumerate and query mapped drives. The following code enumerates mapped drives, extracts the server name portion, and prints that out.
using System; using System.Text.RegularExpressions; using System.Management; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { ManagementObjectSearcher searcher = new ManagementObjectSearcher( "select * from Win32_MappedLogicalDisk"); foreach (ManagementObject drive in searcher.Get()) { Console.WriteLine(Regex.Match( drive["ProviderName"].ToString(), @"\\\\([^\\]+)").Groups[1]); } } } } }
You can find the documentaiton of the Win32_MappedLogicalDisk class here. An intro for accessing WMI from C# is here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With