2013-08-19 80 views
0

我試圖找到一個特定的USB設備(1或更多)連接到我的電腦,並檢索相關的路徑到裝入的驅動器。理想情況下,它將通過查找USB設備的VID/PID,但我不確定如何執行此操作。以下工作,但必須有一些方法來獲取單個查詢中的數據。管理對象查詢Windows(C#)來查找USB設備

我在做什麼這裏正在或具有模型匹配HS SD Card Bridge USB Device,並找到相關的物理驅動器#並用它來找到安裝分區的物理驅動器..

 foreach (ManagementObject disk in disks.Get()) { 
      //look for drives that match our string 
      Match m = Regex.Match(disk["model"].ToString(), "HS SD Card Bridge USB Device"); 
      if (m.Success) { 
       m = Regex.Match(disk["DeviceID"].ToString(), @"PHYSICALDRIVE(\d+)"); 
       if (m.Success) { 
        int driveNumber = Int32.Parse(m.Groups[1].ToString()); 
        ManagementObjectSearcher mapping = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDiskToPartition"); 
        foreach (ManagementObject map in mapping.Get()) { 
         m = Regex.Match(map["Antecedent"].ToString(), @"Disk #" + driveNumber + ","); 
         if (m.Success) { 
          string drive = map["Dependent"].ToString(); 
          m = Regex.Match(drive, @"([A-Z]):"); 
          if (m.Success) { 
           drive = m.Groups[1].ToString(); //< -- **FOUND** 
          } 
         } 

        } 
        //USBDevice dev = new USBDevice("", ""); 
        // list.Items.Add(); 
        Console.WriteLine(""); 
       } 
      } 
} 

是有辦法要從VID/PID做到這一點,並構建搜索查詢的方式,因此它只需要一個查詢?

回答

0

這是我以前用過的。這不會是答案。但會幫助你。

public int GetAvailableDisks() 
     { 
      int deviceFound = 0; 
      try 
      { 
       // browse all USB WMI physical disks 
       foreach (ManagementObject drive in 
        new ManagementObjectSearcher(
         "select DeviceID, Model from Win32_DiskDrive where InterfaceType='USB'").Get()) 
       { 
        ManagementObject partition = new ManagementObjectSearcher(String.Format(
         "associators of {{Win32_DiskDrive.DeviceID='{0}'}} where AssocClass = Win32_DiskDriveToDiskPartition", 
         drive["DeviceID"])).First(); 
        if (partition == null) continue; 
        // associate partitions with logical disks (drive letter volumes) 
        ManagementObject logical = new ManagementObjectSearcher(String.Format(
         "associators of {{Win32_DiskPartition.DeviceID='{0}'}} where AssocClass = Win32_LogicalDiskToPartition", 
         partition["DeviceID"])).First(); 
        if (logical != null) 
        { 
         // finally find the logical disk entry to determine the volume name - Not necesssary 
         //ManagementObject volume = new ManagementObjectSearcher(String.Format(
         // "select FreeSpace, Size, VolumeName from Win32_LogicalDisk where Name='{0}'", 
         // logical["Name"])).First(); 

         string temp = logical["Name"].ToString() + "\\"; 
         // +" " + volume["VolumeName"].ToString(); Future purpose if Device Name required 

         deviceFound++; 
         if (deviceFound > 1) 
         { 
          MessageBox.Show(@"Multiple Removeable media found. Please remove the another device"); 
          deviceFound--; 

         } 
         else 
         { 
          driveName = temp; 
         } 

        } 
       } 
      } 
      catch (Exception diskEnumerateException) 
      { 

      } 
      return deviceFound; 
     }