Reputation: 581
Can anyone suggest how to list the following, preferably in .net
?
Driver Letter, Device Driver
I can get some fairly basic information about drives connected to my computer using:
DriveInfo[] drives = DriveInfo.GetDrives();
I can get more information using WMI, but I can't get the device driver associated with each drive:
SelectQuery query = new SelectQuery("select * from win32_DiskDrive");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
I can list device id's with their drivers using Win.OBJECT_DIRECTORY_INFORMATION
, however I can't then map these to drives.
Upvotes: 1
Views: 1219
Reputation: 581
I found what I needed with the following function, from http://bloggingabout.net/blogs/ramon/archive/2007/04/05/get-the-physical-path-of-a-path-that-uses-a-subst-drive.aspx
private static string GetRealPath(string path)
{
string realPath = path;
StringBuilder pathInformation = new StringBuilder(250);
string driveLetter = Path.GetPathRoot(realPath).Replace("\\", "");
QueryDosDevice(driveLetter, pathInformation, 250);
// If drive is substed, the result will be in the format of "\??\C:\RealPath\".
// Strip the \??\ prefix.
string realRoot = pathInformation.ToString().Remove(0, 4);
//Combine the paths.
realPath = Path.Combine(realRoot, realPath.Replace(Path.GetPathRoot(realPath), ""));
return realPath;
}
[DllImport("kernel32.dll")]
static extern uint QueryDosDevice(string lpDeviceName, StringBuilder lpTargetPath, int ucchMax);
Upvotes: 1