List mapped drives on Remote Machine

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a .NET method for doing this? I haven't found anything else that
works.

Thanks
 
Add reference to the .net library "System.Management"

Add the using statement:
using System.Management;

Add this procedure:

private List<string> GetMappedDrives( string computer )
{
List<string> toReturn = new List<string>();

ConnectionOptions options = new ConnectionOptions();
options.Impersonation =
System.Management.ImpersonationLevel.Impersonate;

//Path for the query, specifying which computer to use
ManagementPath basePath = new ManagementPath( @"\\" + computer +
@"\ROOT\CIMV2" );

//Select all logical drives that are of type 4 which is a
network drive
//Other choices would be:
//0 Unknown
//1 No Root Directory
//2 Removable Disk
//3 Local Disk
//4 Network Drive
//5 Compact Disc
//6 RAM Disk
ObjectQuery query = new ObjectQuery( "SELECT * FROM
Win32_LogicalDisk WHERE DriveType = 4" );

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

//Loop through returned drives
foreach ( ManagementObject drive in searcher.Get() )
toReturn.Add( drive.Properties["Name"].Value.ToString() );

return toReturn;
}

This connects to WMI on the remote machine and enumerates the mapped drives
on it. You need permission to do this on the machine, so make sure you are
logged onto your machine under an account with admin access to the remote
machine.

Chris Balmer
Network Administrator
Concord International
 
I revised the procedure to use a different query string:

private List<string> GetMappedDrives( string computer )
{
List<string> toReturn = new List<string>();

ConnectionOptions options = new ConnectionOptions();
options.Impersonation =
System.Management.ImpersonationLevel.Impersonate;

//Path for the query, specifying which computer to use
ManagementPath basePath = new ManagementPath( @"\\" + computer +
@"\ROOT\CIMV2" );

ObjectQuery query = new ObjectQuery( "SELECT * FROM
Win32_MappedLogicalDisk" );
ManagementScope scope = new ManagementScope( basePath, options );
ManagementObjectSearcher searcher = new
ManagementObjectSearcher( scope, query );

//Loop through returned drives
foreach ( ManagementObject drive in searcher.Get() )
toReturn.Add( drive.Properties["Name"].Value.ToString() );

return toReturn;
}

balmerch said:
Add reference to the .net library "System.Management"

Add the using statement:
using System.Management;

Add this procedure:

private List<string> GetMappedDrives( string computer )
{
List<string> toReturn = new List<string>();

ConnectionOptions options = new ConnectionOptions();
options.Impersonation =
System.Management.ImpersonationLevel.Impersonate;

//Path for the query, specifying which computer to use
ManagementPath basePath = new ManagementPath( @"\\" + computer +
@"\ROOT\CIMV2" );

//Select all logical drives that are of type 4 which is a
network drive
//Other choices would be:
//0 Unknown
//1 No Root Directory
//2 Removable Disk
//3 Local Disk
//4 Network Drive
//5 Compact Disc
//6 RAM Disk
ObjectQuery query = new ObjectQuery( "SELECT * FROM
Win32_LogicalDisk WHERE DriveType = 4" );

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

//Loop through returned drives
foreach ( ManagementObject drive in searcher.Get() )
toReturn.Add( drive.Properties["Name"].Value.ToString() );

return toReturn;
}

This connects to WMI on the remote machine and enumerates the mapped drives
on it. You need permission to do this on the machine, so make sure you are
logged onto your machine under an account with admin access to the remote
machine.

Chris Balmer
Network Administrator
Concord International

lastusernameleft said:
Is there a .NET method for doing this? I haven't found anything else that
works.

Thanks
 
Back
Top