2011-11-28 84 views

回答

4

下面是我根據迄今爲止發現的內容編寫的一些代碼。可能有更好的方法來做到這一點。

public static void MyMethod() 
    {        
     System.Management.ManagementClass USBClass = new ManagementClass("Win32_USBDevice");     
     System.Management.ManagementObjectCollection USBCollection = USBClass.GetInstances(); 

     foreach (System.Management.ManagementObject usb in USBCollection) 
     { 
      string deviceId = usb["deviceid"].ToString(); 
      Console.WriteLine(deviceId); 

      int vidIndex = deviceId.IndexOf("VID_"); 
      string startingAtVid = deviceId.Substring(vidIndex + 4); // + 4 to remove "VID_"      
      string vid = startingAtVid.Substring(0, 4); // vid is four characters long 
      Console.WriteLine("VID: " + vid); 

      int pidIndex = deviceId.IndexOf("PID_"); 
      string startingAtPid = deviceId.Substring(pidIndex + 4); // + 4 to remove "PID_"      
      string pid = startingAtPid.Substring(0, 4); // pid is four characters long 
      Console.WriteLine("PID: " + pid);           
     }    

     Console.ReadLine(); 
    } 
相關問題