【问题标题】:List Disk Drives with their Driver in C#在 C# 中列出磁盘驱动器及其驱动程序
【发布时间】:2013-06-23 16:31:49
【问题描述】:

谁能建议如何列出以下内容,最好是.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 及其驱动程序,但是我无法将它们映射到驱动器。

【问题讨论】:

    标签: c# winapi kernel wmi drivers


    【解决方案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);
    

    【讨论】: