2017-11-21 2 views

答えて

0

、あなたはこのような何か試すことができ、あなたのプロジェクトでSystem.Managementへの参照を追加してください:

namespace ConsoleApplication1 { 
    using System; 
    using System.Collections.Generic; 
    using System.Management; // need to add System.Management to your project references. 

    class Program { 
     static void Main(string[] args) { 
      var usbDevices = GetUSBDevices(); 

      foreach(var usbDevice in usbDevices) { 
       Console.WriteLine("Device ID: {0}, PNP Device ID: {1}, Description: {2}", usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description); 
      } 

      Console.Read(); 
     } 

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

      ManagementObjectCollection collection; 
      using(var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub")) 
      collection = searcher.Get(); 

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

      collection.Dispose(); 
      return devices; 
     } 
    } 

    class USBDeviceInfo { 
     public USBDeviceInfo(string deviceID, string pnpDeviceID, string description) { 
      this.DeviceID = deviceID; 
      this.PnpDeviceID = pnpDeviceID; 
      this.Description = description; 
     } 
     public string DeviceID { 
      get; 
      private set; 
     } 
     public string PnpDeviceID { 
      get; 
      private set; 
     } 
     public string Description { 
      get; 
      private set; 
     } 
    } 
} 

編集:フレンドリー名

ManagementObjectCollection mbsList = null; 
      ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select * From Win32_USBHub"); 
      mbsList = mbs.Get(); 

      foreach (ManagementObject mo in mbsList) 
      { 
       Console.WriteLine("USBHub device Friendly name:{0}", mo["Name"].ToString()); 
      } 
+0

感謝を。 この部分的に機能しますが、コントロールパネルで指定されたモデル名を取得するにはどうすればよいですか? –

+0

モデルは?デバイスのモデルのように? – diomonogatari

+0

私のanwserを編集しました。それを試してみてください – diomonogatari

関連する問題