Code:
using System; using System.Windows.Forms; using System.Linq; using System.Management; namespace Sample { public partial class Form1 : Form { public Form1() { InitializeComponent(); ManagementObjectSearcher search = new ManagementObjectSearcher("SELECT * FROM Win32_USBHub"); ManagementObjectCollection collection = search.Get(); var usbList = from u in collection.Cast<ManagementBaseObject>() select new { id = u.GetPropertyValue("DeviceID"), name=u.GetPropertyValue("Name"), status = u.GetPropertyValue("Status"), system=u.GetPropertyValue("SystemName"), caption = u.GetPropertyValue("Caption"), pnp = u.GetPropertyValue("PNPDeviceID"), description = u.GetPropertyValue("Description") }; foreach (var u in usbList) textBox1.Text += String.Format("{0}{7}{1}{7}{2}{7}{3}{7}{4}{7}{5}{7}{6}{7}{7}{7}", u.id, u.name,u.status,u.system,u.caption,u.pnp,u.description, Environment.NewLine); } } }
執行結果:
參考資料:
https://msdn.microsoft.com/en-us/library/aa394504(v=vs.85).aspx
http://stackoverflow.com/questions/3331043/get-list-of-connected-usb-devices
http://stackoverflow.com/questions/27965098/c-sharp-how-do-i-check-usb-ports-with-usb-hub-and-used-controller
https://msdn.microsoft.com/en-us/library/windows/hardware/ff539267(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/hardware/dn303344(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/hardware/dn303349(v=vs.85).aspx
https://msdn.microsoft.com/library/windows/apps/dn263883?cs-save-lang=1&cs-lang=csharp#code-snippet-1