2017-08-27 95 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; } 
    } 
}