WMIUsbQuery.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Management;
  5. using System.Text.RegularExpressions;
  6. namespace LYFZ.WinAPI
  7. {
  8. /// <summary>
  9. /// 即插即用设备信息结构
  10. /// </summary>
  11. public struct PnPEntityInfo
  12. {
  13. public String PNPDeviceID; // 设备ID
  14. public String Name; // 设备名称
  15. public String Description; // 设备描述
  16. public String Service; // 服务
  17. public String Status; // 设备状态
  18. public UInt16 VendorID; // 供应商标识
  19. public UInt16 ProductID; // 产品编号
  20. public Guid ClassGuid; // 设备安装类GUID
  21. }
  22. /// <summary>
  23. /// 基于WMI获取USB设备信息
  24. /// </summary>
  25. public partial class WMIUsbQuery
  26. {
  27. #region UsbDevice
  28. /// <summary>
  29. /// 获取所有的USB设备实体(过滤没有VID和PID的设备)
  30. /// </summary>
  31. public static PnPEntityInfo[] AllUsbDevices
  32. {
  33. get
  34. {
  35. return WhoUsbDevice(UInt16.MinValue, UInt16.MinValue, Guid.Empty);
  36. }
  37. }
  38. /// <summary>
  39. /// 查询USB设备实体(设备要求有VID和PID)
  40. /// </summary>
  41. /// <param name="VendorID">供应商标识,MinValue忽视</param>
  42. /// <param name="ProductID">产品编号,MinValue忽视</param>
  43. /// <param name="ClassGuid">设备安装类Guid,Empty忽视</param>
  44. /// <returns>设备列表</returns>
  45. public static PnPEntityInfo[] WhoUsbDevice(UInt16 VendorID, UInt16 ProductID, Guid ClassGuid)
  46. {
  47. List<PnPEntityInfo> UsbDevices = new List<PnPEntityInfo>();
  48. // 获取USB控制器及其相关联的设备实体
  49. ManagementObjectCollection USBControllerDeviceCollection = new ManagementObjectSearcher("SELECT * FROM Win32_USBControllerDevice").Get();
  50. if (USBControllerDeviceCollection != null)
  51. {
  52. foreach (ManagementObject USBControllerDevice in USBControllerDeviceCollection)
  53. { // 获取设备实体的DeviceID
  54. String Dependent = (USBControllerDevice["Dependent"] as String).Split(new Char[] { '=' })[1];
  55. // 过滤掉没有VID和PID的USB设备
  56. Match match = Regex.Match(Dependent, "VID_[0-9|A-F]{4}&PID_[0-9|A-F]{4}");
  57. if (match.Success)
  58. {
  59. UInt16 theVendorID = Convert.ToUInt16(match.Value.Substring(4, 4), 16); // 供应商标识
  60. if (VendorID != UInt16.MinValue && VendorID != theVendorID) continue;
  61. UInt16 theProductID = Convert.ToUInt16(match.Value.Substring(13, 4), 16); // 产品编号
  62. if (ProductID != UInt16.MinValue && ProductID != theProductID) continue;
  63. ManagementObjectCollection PnPEntityCollection = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE DeviceID=" + Dependent).Get();
  64. if (PnPEntityCollection != null)
  65. {
  66. foreach (ManagementObject Entity in PnPEntityCollection)
  67. {
  68. Guid theClassGuid = new Guid(Entity["ClassGuid"] as String); // 设备安装类GUID
  69. if (ClassGuid != Guid.Empty && ClassGuid != theClassGuid) continue;
  70. PnPEntityInfo Element;
  71. Element.PNPDeviceID = Entity["PNPDeviceID"] as String; // 设备ID
  72. Element.Name = Entity["Name"] as String; // 设备名称
  73. Element.Description = Entity["Description"] as String; // 设备描述
  74. Element.Service = Entity["Service"] as String; // 服务
  75. Element.Status = Entity["Status"] as String; // 设备状态
  76. Element.VendorID = theVendorID; // 供应商标识
  77. Element.ProductID = theProductID; // 产品编号
  78. Element.ClassGuid = theClassGuid; // 设备安装类GUID
  79. UsbDevices.Add(Element);
  80. }
  81. }
  82. }
  83. }
  84. }
  85. if (UsbDevices.Count == 0) return null; else return UsbDevices.ToArray();
  86. }
  87. /// <summary>
  88. /// 查询USB设备实体(设备要求有VID和PID)
  89. /// </summary>
  90. /// <param name="VendorID">供应商标识,MinValue忽视</param>
  91. /// <param name="ProductID">产品编号,MinValue忽视</param>
  92. /// <returns>设备列表</returns>
  93. public static PnPEntityInfo[] WhoUsbDevice(UInt16 VendorID, UInt16 ProductID)
  94. {
  95. return WhoUsbDevice(VendorID, ProductID, Guid.Empty);
  96. }
  97. /// <summary>
  98. /// 查询USB设备实体(设备要求有VID和PID)
  99. /// </summary>
  100. /// <param name="ClassGuid">设备安装类Guid,Empty忽视</param>
  101. /// <returns>设备列表</returns>
  102. public static PnPEntityInfo[] WhoUsbDevice(Guid ClassGuid)
  103. {
  104. return WhoUsbDevice(UInt16.MinValue, UInt16.MinValue, ClassGuid);
  105. }
  106. /// <summary>
  107. /// 查询USB设备实体(设备要求有VID和PID)
  108. /// </summary>
  109. /// <param name="PNPDeviceID">设备ID,可以是不完整信息</param>
  110. /// <returns>设备列表</returns>
  111. public static PnPEntityInfo[] WhoUsbDevice(String PNPDeviceID)
  112. {
  113. List<PnPEntityInfo> UsbDevices = new List<PnPEntityInfo>();
  114. // 获取USB控制器及其相关联的设备实体
  115. ManagementObjectCollection USBControllerDeviceCollection = new ManagementObjectSearcher("SELECT * FROM Win32_USBControllerDevice").Get();
  116. if (USBControllerDeviceCollection != null)
  117. {
  118. foreach (ManagementObject USBControllerDevice in USBControllerDeviceCollection)
  119. { // 获取设备实体的DeviceID
  120. String Dependent = (USBControllerDevice["Dependent"] as String).Split(new Char[] { '=' })[1];
  121. if (!String.IsNullOrEmpty(PNPDeviceID))
  122. { // 注意:忽视大小写
  123. if (Dependent.IndexOf(PNPDeviceID, 1, PNPDeviceID.Length - 2, StringComparison.OrdinalIgnoreCase) == -1) continue;
  124. }
  125. // 过滤掉没有VID和PID的USB设备
  126. Match match = Regex.Match(Dependent, "VID_[0-9|A-F]{4}&PID_[0-9|A-F]{4}");
  127. if (match.Success)
  128. {
  129. ManagementObjectCollection PnPEntityCollection = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE DeviceID=" + Dependent).Get();
  130. if (PnPEntityCollection != null)
  131. {
  132. foreach (ManagementObject Entity in PnPEntityCollection)
  133. {
  134. PnPEntityInfo Element;
  135. Element.PNPDeviceID = Entity["PNPDeviceID"] as String; // 设备ID
  136. Element.Name = Entity["Name"] as String; // 设备名称
  137. Element.Description = Entity["Description"] as String; // 设备描述
  138. Element.Service = Entity["Service"] as String; // 服务
  139. Element.Status = Entity["Status"] as String; // 设备状态
  140. Element.VendorID = Convert.ToUInt16(match.Value.Substring(4, 4), 16); // 供应商标识
  141. Element.ProductID = Convert.ToUInt16(match.Value.Substring(13, 4), 16); // 产品编号 // 产品编号
  142. Element.ClassGuid = new Guid(Entity["ClassGuid"] as String); // 设备安装类GUID
  143. UsbDevices.Add(Element);
  144. }
  145. }
  146. }
  147. }
  148. }
  149. if (UsbDevices.Count == 0) return null; else return UsbDevices.ToArray();
  150. }
  151. /// <summary>
  152. /// 根据服务定位USB设备
  153. /// </summary>
  154. /// <param name="ServiceCollection">要查询的服务集合</param>
  155. /// <returns>设备列表</returns>
  156. public static PnPEntityInfo[] WhoUsbDevice(String[] ServiceCollection)
  157. {
  158. if (ServiceCollection == null || ServiceCollection.Length == 0)
  159. return WhoUsbDevice(UInt16.MinValue, UInt16.MinValue, Guid.Empty);
  160. List<PnPEntityInfo> UsbDevices = new List<PnPEntityInfo>();
  161. // 获取USB控制器及其相关联的设备实体
  162. ManagementObjectCollection USBControllerDeviceCollection = new ManagementObjectSearcher("SELECT * FROM Win32_USBControllerDevice").Get();
  163. if (USBControllerDeviceCollection != null)
  164. {
  165. foreach (ManagementObject USBControllerDevice in USBControllerDeviceCollection)
  166. { // 获取设备实体的DeviceID
  167. String Dependent = (USBControllerDevice["Dependent"] as String).Split(new Char[] { '=' })[1];
  168. // 过滤掉没有VID和PID的USB设备
  169. Match match = Regex.Match(Dependent, "VID_[0-9|A-F]{4}&PID_[0-9|A-F]{4}");
  170. if (match.Success)
  171. {
  172. ManagementObjectCollection PnPEntityCollection = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE DeviceID=" + Dependent).Get();
  173. if (PnPEntityCollection != null)
  174. {
  175. foreach (ManagementObject Entity in PnPEntityCollection)
  176. {
  177. String theService = Entity["Service"] as String; // 服务
  178. if (String.IsNullOrEmpty(theService)) continue;
  179. foreach (String Service in ServiceCollection)
  180. { // 注意:忽视大小写
  181. if (String.Compare(theService, Service, true) != 0) continue;
  182. PnPEntityInfo Element;
  183. Element.PNPDeviceID = Entity["PNPDeviceID"] as String; // 设备ID
  184. Element.Name = Entity["Name"] as String; // 设备名称
  185. Element.Description = Entity["Description"] as String; // 设备描述
  186. Element.Service = theService; // 服务
  187. Element.Status = Entity["Status"] as String; // 设备状态
  188. Element.VendorID = Convert.ToUInt16(match.Value.Substring(4, 4), 16); // 供应商标识
  189. Element.ProductID = Convert.ToUInt16(match.Value.Substring(13, 4), 16); // 产品编号
  190. Element.ClassGuid = new Guid(Entity["ClassGuid"] as String); // 设备安装类GUID
  191. UsbDevices.Add(Element);
  192. break;
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. if (UsbDevices.Count == 0) return null; else return UsbDevices.ToArray();
  200. }
  201. #endregion
  202. #region PnPEntity
  203. /// <summary>
  204. /// 所有即插即用设备实体(过滤没有VID和PID的设备)
  205. /// </summary>
  206. public static PnPEntityInfo[] AllPnPEntities
  207. {
  208. get
  209. {
  210. return WhoPnPEntity(UInt16.MinValue, UInt16.MinValue, Guid.Empty);
  211. }
  212. }
  213. /// <summary>
  214. /// 根据VID和PID及设备安装类GUID定位即插即用设备实体
  215. /// </summary>
  216. /// <param name="VendorID">供应商标识,MinValue忽视</param>
  217. /// <param name="ProductID">产品编号,MinValue忽视</param>
  218. /// <param name="ClassGuid">设备安装类Guid,Empty忽视</param>
  219. /// <returns>设备列表</returns>
  220. /// <remarks>
  221. /// HID:{745a17a0-74d3-11d0-b6fe-00a0c90f57da}
  222. /// Imaging Device:{6bdd1fc6-810f-11d0-bec7-08002be2092f}
  223. /// Keyboard:{4d36e96b-e325-11ce-bfc1-08002be10318}
  224. /// Mouse:{4d36e96f-e325-11ce-bfc1-08002be10318}
  225. /// Network Adapter:{4d36e972-e325-11ce-bfc1-08002be10318}
  226. /// USB:{36fc9e60-c465-11cf-8056-444553540000}
  227. /// </remarks>
  228. public static PnPEntityInfo[] WhoPnPEntity(UInt16 VendorID, UInt16 ProductID, Guid ClassGuid)
  229. {
  230. List<PnPEntityInfo> PnPEntities = new List<PnPEntityInfo>();
  231. // 枚举即插即用设备实体
  232. String VIDPID;
  233. if (VendorID == UInt16.MinValue)
  234. {
  235. if (ProductID == UInt16.MinValue)
  236. VIDPID = "'%VID[_]____&PID[_]____%'";
  237. else
  238. VIDPID = "'%VID[_]____&PID[_]" + ProductID.ToString("X4") + "%'";
  239. }
  240. else
  241. {
  242. if (ProductID == UInt16.MinValue)
  243. VIDPID = "'%VID[_]" + VendorID.ToString("X4") + "&PID[_]____%'";
  244. else
  245. VIDPID = "'%VID[_]" + VendorID.ToString("X4") + "&PID[_]" + ProductID.ToString("X4") + "%'";
  246. }
  247. String QueryString;
  248. if (ClassGuid == Guid.Empty)
  249. QueryString = "SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID LIKE" + VIDPID;
  250. else
  251. QueryString = "SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID LIKE" + VIDPID + " AND ClassGuid='" + ClassGuid.ToString("B") + "'";
  252. ManagementObjectCollection PnPEntityCollection = new ManagementObjectSearcher(QueryString).Get();
  253. if (PnPEntityCollection != null)
  254. {
  255. foreach (ManagementObject Entity in PnPEntityCollection)
  256. {
  257. String PNPDeviceID = Entity["PNPDeviceID"] as String;
  258. Match match = Regex.Match(PNPDeviceID, "VID_[0-9|A-F]{4}&PID_[0-9|A-F]{4}");
  259. if (match.Success)
  260. {
  261. PnPEntityInfo Element;
  262. Element.PNPDeviceID = PNPDeviceID; // 设备ID
  263. Element.Name = Entity["Name"] as String; // 设备名称
  264. Element.Description = Entity["Description"] as String; // 设备描述
  265. Element.Service = Entity["Service"] as String; // 服务
  266. Element.Status = Entity["Status"] as String; // 设备状态
  267. Element.VendorID = Convert.ToUInt16(match.Value.Substring(4, 4), 16); // 供应商标识
  268. Element.ProductID = Convert.ToUInt16(match.Value.Substring(13, 4), 16); // 产品编号
  269. Element.ClassGuid = new Guid(Entity["ClassGuid"] as String); // 设备安装类GUID
  270. PnPEntities.Add(Element);
  271. }
  272. }
  273. }
  274. if (PnPEntities.Count == 0) return null; else return PnPEntities.ToArray();
  275. }
  276. /// <summary>
  277. /// 根据VID和PID定位即插即用设备实体
  278. /// </summary>
  279. /// <param name="VendorID">供应商标识,MinValue忽视</param>
  280. /// <param name="ProductID">产品编号,MinValue忽视</param>
  281. /// <returns>设备列表</returns>
  282. public static PnPEntityInfo[] WhoPnPEntity(UInt16 VendorID, UInt16 ProductID)
  283. {
  284. return WhoPnPEntity(VendorID, ProductID, Guid.Empty);
  285. }
  286. /// <summary>
  287. /// 根据设备安装类GUID定位即插即用设备实体
  288. /// </summary>
  289. /// <param name="ClassGuid">设备安装类Guid,Empty忽视</param>
  290. /// <returns>设备列表</returns>
  291. public static PnPEntityInfo[] WhoPnPEntity(Guid ClassGuid)
  292. {
  293. return WhoPnPEntity(UInt16.MinValue, UInt16.MinValue, ClassGuid);
  294. }
  295. /// <summary>
  296. /// 根据设备ID定位设备
  297. /// </summary>
  298. /// <param name="PNPDeviceID">设备ID,可以是不完整信息</param>
  299. /// <returns>设备列表</returns>
  300. /// <remarks>
  301. /// 注意:对于下划线,需要写成“[_]”,否则视为任意字符
  302. /// </remarks>
  303. public static PnPEntityInfo[] WhoPnPEntity(String PNPDeviceID)
  304. {
  305. List<PnPEntityInfo> PnPEntities = new List<PnPEntityInfo>();
  306. // 枚举即插即用设备实体
  307. String QueryString;
  308. if (String.IsNullOrEmpty(PNPDeviceID))
  309. {
  310. QueryString = "SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID LIKE '%VID[_]____&PID[_]____%'";
  311. }
  312. else
  313. { // LIKE子句中有反斜杠字符将会引发WQL查询异常
  314. QueryString = "SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID LIKE '%" + PNPDeviceID.Replace('\\', '_') + "%'";
  315. }
  316. ManagementObjectCollection PnPEntityCollection = new ManagementObjectSearcher(QueryString).Get();
  317. if (PnPEntityCollection != null)
  318. {
  319. foreach (ManagementObject Entity in PnPEntityCollection)
  320. {
  321. String thePNPDeviceID = Entity["PNPDeviceID"] as String;
  322. Match match = Regex.Match(thePNPDeviceID, "VID_[0-9|A-F]{4}&PID_[0-9|A-F]{4}");
  323. if (match.Success)
  324. {
  325. PnPEntityInfo Element;
  326. Element.PNPDeviceID = thePNPDeviceID; // 设备ID
  327. Element.Name = Entity["Name"] as String; // 设备名称
  328. Element.Description = Entity["Description"] as String; // 设备描述
  329. Element.Service = Entity["Service"] as String; // 服务
  330. Element.Status = Entity["Status"] as String; // 设备状态
  331. Element.VendorID = Convert.ToUInt16(match.Value.Substring(4, 4), 16); // 供应商标识
  332. Element.ProductID = Convert.ToUInt16(match.Value.Substring(13, 4), 16); // 产品编号
  333. Element.ClassGuid = new Guid(Entity["ClassGuid"] as String); // 设备安装类GUID
  334. PnPEntities.Add(Element);
  335. }
  336. }
  337. }
  338. if (PnPEntities.Count == 0) return null; else return PnPEntities.ToArray();
  339. }
  340. /// <summary>
  341. /// 根据服务定位设备
  342. /// </summary>
  343. /// <param name="ServiceCollection">要查询的服务集合,null忽视</param>
  344. /// <returns>设备列表</returns>
  345. /// <remarks>
  346. /// 跟服务相关的类:
  347. /// Win32_SystemDriverPNPEntity
  348. /// Win32_SystemDriver
  349. /// </remarks>
  350. public static PnPEntityInfo[] WhoPnPEntity(String[] ServiceCollection)
  351. {
  352. if (ServiceCollection == null || ServiceCollection.Length == 0)
  353. return WhoPnPEntity(UInt16.MinValue, UInt16.MinValue, Guid.Empty);
  354. List<PnPEntityInfo> PnPEntities = new List<PnPEntityInfo>();
  355. // 枚举即插即用设备实体
  356. String QueryString = "SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID LIKE '%VID[_]____&PID[_]____%'";
  357. ManagementObjectCollection PnPEntityCollection = new ManagementObjectSearcher(QueryString).Get();
  358. if (PnPEntityCollection != null)
  359. {
  360. foreach (ManagementObject Entity in PnPEntityCollection)
  361. {
  362. String PNPDeviceID = Entity["PNPDeviceID"] as String;
  363. Match match = Regex.Match(PNPDeviceID, "VID_[0-9|A-F]{4}&PID_[0-9|A-F]{4}");
  364. if (match.Success)
  365. {
  366. String theService = Entity["Service"] as String; // 服务
  367. if (String.IsNullOrEmpty(theService)) continue;
  368. foreach (String Service in ServiceCollection)
  369. { // 注意:忽视大小写
  370. if (String.Compare(theService, Service, true) != 0) continue;
  371. PnPEntityInfo Element;
  372. Element.PNPDeviceID = PNPDeviceID; // 设备ID
  373. Element.Name = Entity["Name"] as String; // 设备名称
  374. Element.Description = Entity["Description"] as String; // 设备描述
  375. Element.Service = theService; // 服务
  376. Element.Status = Entity["Status"] as String; // 设备状态
  377. Element.VendorID = Convert.ToUInt16(match.Value.Substring(4, 4), 16); // 供应商标识
  378. Element.ProductID = Convert.ToUInt16(match.Value.Substring(13, 4), 16); // 产品编号
  379. Element.ClassGuid = new Guid(Entity["ClassGuid"] as String); // 设备安装类GUID
  380. PnPEntities.Add(Element);
  381. break;
  382. }
  383. }
  384. }
  385. }
  386. if (PnPEntities.Count == 0) return null; else return PnPEntities.ToArray();
  387. }
  388. #endregion
  389. }
  390. }