简体   繁体   中英

List Disk Drives with their Driver in C#

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM