/// <summary> /// 将USB设备枚举信息导出为XML文档 /// </summary> /// <param name="xmlFileName">保存的XML文件名</param> /// <returns> /// true:成功 /// false:失败 /// </returns> public static Boolean EnumUsbToXML(String xmlFileName) { // 创建根节点 XElement RootNode = new XElement("Computer", new XAttribute("MachineName", System.Environment.MachineName)); // 深度遍历主控制器 HostControllerInfo[] HostControllersCollection = USB.AllHostControllers; if (HostControllersCollection != null) { Int32 ControllerIndex = 1; foreach (HostControllerInfo item in HostControllersCollection) { // 创建主控制器节点 XElement HostControllerNode = new XElement("HostController" + ControllerIndex, new XAttribute("Name", item.Name), // 设备名称 new XAttribute("PNPDeviceID", item.PNPDeviceID), // 设备ID new XAttribute("HcdDriverKeyName", item.HcdDriverKeyName) // 驱动键名 ); RootNode.Add(HostControllerNode); ControllerIndex++; // 创建根集线器节点 String RootHubPath = USB.GetUsbRootHubPath(item.PNPDeviceID); AddHubNode(HostControllerNode, RootHubPath, "RootHub"); } } // 创建XML文档 XDocument xmlTree = new XDocument(RootNode); // 存储文件,序列化时对XML进行格式设置(缩进) xmlTree.Save(xmlFileName, SaveOptions.None); return(true); }
/// <summary> /// 增加集线器节点 /// </summary> /// <param name="ParentNode">父节点</param> /// <param name="HubPath">集线器路径</param> private static void AddHubNode(XElement ParentNode, String HubPath, String HubNodeName) { UsbNodeInformation[] NodeInfoCollection = USB.GetUsbNodeInformation(HubPath); if (NodeInfoCollection != null) { USB_HUB_NODE NodeType = NodeInfoCollection[0].NodeType; XElement HubNode = new XElement(HubNodeName, new XAttribute("Name", NodeInfoCollection[0].Name), new XAttribute("PNPDeviceID", NodeInfoCollection[0].PNPDeviceID), new XAttribute("Path", NodeInfoCollection[0].DevicePath), new XAttribute("NodeType", NodeType) ); if (NodeType == USB_HUB_NODE.UsbHub) { Int32 NumberOfPorts = NodeInfoCollection[0].NumberOfPorts; HubNode.Add(new XAttribute("NumberOfPorts", NumberOfPorts), new XAttribute("HubIsBusPowered", NodeInfoCollection[0].HubIsBusPowered), new XAttribute("HubCharacteristics", "0x" + NodeInfoCollection[0].HubCharacteristics.ToString("X4")), new XAttribute("PowerOnToPowerGood", NodeInfoCollection[0].PowerOnToPowerGood), new XAttribute("HubControlCurrent", NodeInfoCollection[0].HubControlCurrent) ); // 深度遍历端口 UsbNodeConnectionInformation[] NodeConnectionInfoCollection = USB.GetUsbNodeConnectionInformation(HubPath, NumberOfPorts); if (NodeConnectionInfoCollection != null) { foreach (UsbNodeConnectionInformation NodeConnectionInfo in NodeConnectionInfoCollection) { // 增加端口节点 AddPortNode(HubNode, NodeConnectionInfo); } } } else { HubNode.Add("NumberOfInterfaces", NodeInfoCollection[0].NumberOfInterfaces); } ParentNode.Add(HubNode); } }