使用C#中的驱动程序列出磁盘驱动器

时间:2013-06-23 16:31:50

标签: c# winapi kernel wmi drivers

有人可以建议如何列出以下内容,最好是.net吗?

 Driver Letter, Device Driver

我可以使用以下方式获得有关连接到计算机的驱动器的一些相当基本的信息:

DriveInfo[] drives = DriveInfo.GetDrives();

我可以使用WMI获取更多信息,但我无法获得与每个驱动器关联的设备驱动程序:

 SelectQuery query = new SelectQuery("select * from win32_DiskDrive");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);

我可以使用Win.OBJECT_DIRECTORY_INFORMATION列出其驱动程序的设备ID,但是我无法将这些ID映射到驱动器。

1 个答案:

答案 0 :(得分:1)

我从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);
相关问题