2017-08-27 77 views
0

如何查詢「設備和打印機」菜單中未顯示管理員設置的設備名稱?查詢「設備和打印機」菜單中顯示的設備名稱

這不是設備的友好名稱,我首先想到的是,這樣WMI/ManagementObjectSearcher是沒有幫助的,因爲它沒有簡單的包含該信息的所有的任何地方(或IM無法找到)

只有我發現這個信息是在註冊表下「Computer \ HKEY_LOCAL_MACHINE \ SYSTEM \ ControlSet001 \ Control \ DeviceMigration \ Devices \ USB \ VID_0403 & PID_6001 \ XXXX ** BusDeviceDesc **」這將是很好,因爲我可以看到哪些Comports是活躍的,如果它在那裏找到相同的端口,我認爲它是我正在尋找的那個,但是這裏的問題在於它確實需要管理員私有化來挖掘註冊表,這是我想避免的。

所以有沒有一種方法來識別USB設備,我正在尋找沒有管理員privacyledges,沒有我buding自定義vid/PID從FTDI這顯然會很容易,但花費相當多的錢爲愛好項目。

**我無法添加圖片,使之更加明確

RegistryKey key = Registry.LocalMachine; 
     key = key.OpenSubKey(@"SYSTEM\ControlSet001\Control\DeviceMigration\Devices\USB\VID_0403&PID_6001", true); 

ManagementObjectSearcher(@"SELECT * FROM Win32_PnPEntity where DeviceID Like ""USB%""")) 

這是我目前如何尋找信息,然後交叉檢查的結果,以確定我想連接到設備,但需要管理員權限。

回答

0

所以經過一番研究,我沒有找到一個方法來做到我想要的,我在這裏發佈的代碼,因爲它可能會幫助別人的未來是什麼。(遺憾的是這僅實施幫助,如果使用FTDI IC)

class Program { 
    static void Main(string[] args) { 

     string DeviceActualName = "Fan Control"; 


     FTDI usbDev = new FTDI(); 
     UInt32 devCount = 0; 
     usbDev.GetNumberOfDevices(ref devCount); 
     FTDI.FT_DEVICE_INFO_NODE[] infoNode = new FTDI.FT_DEVICE_INFO_NODE[devCount]; 

     string SerialNumber = null; 
     usbDev.GetDeviceList(infoNode); 
     foreach(FTDI.FT_DEVICE_INFO_NODE node in infoNode) { 
      if(node != null && node.Description.Equals(DeviceActualName)) { 
       Console.WriteLine("Found: {0} // {1}", node.Description, node.SerialNumber); 
       SerialNumber = node.SerialNumber; 
      } 
     } 

     var usbDevices = GetUSBDevices(); 
     foreach(var usbDevice in usbDevices) { 
      if(usbDevice.Name != null) 
       if(usbDevice.Name.Contains("COM") && usbDevice.PnpDeviceID.Contains(SerialNumber)) { 
        Console.WriteLine("Match Found: {0} // {1}", usbDevice.Name, usbDevice.PnpDeviceID); 
        Console.WriteLine("ComPort: {0}", usbDevice.Name[(usbDevice.Name.IndexOf("COM") + 3)]); 
       } 
     } 

     Console.Read(); 
    } 

    static List<USBDeviceInfo> GetUSBDevices() { 
     List<USBDeviceInfo> devices = new List<USBDeviceInfo>(); 

     ManagementObjectCollection collection; 
     using(var searcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_PnPEntity")) 
      collection = searcher.Get(); 

     foreach(var device in collection) { 
      devices.Add(new USBDeviceInfo(
      (string) device.GetPropertyValue("Name"), 
      (string) device.GetPropertyValue("PNPDeviceID") 
      )); 
     } 

     collection.Dispose(); 
     return devices; 
    } 
    class USBDeviceInfo { 
     public USBDeviceInfo(string Name, string pnpDeviceID) { 
      this.Name = Name; 
      this.PnpDeviceID = pnpDeviceID; 
     } 
     public string Name { get; private set; } 
     public string PnpDeviceID { get; private set; } 
    } 
}