C#:USB设备枚举(三)输出枚举信息到XML文档

215 篇文章 2 订阅
120 篇文章 3 订阅

作者:Splash

转自:http://blog.csdn.net/jhqin/article/details/6918154


[csharp] view plain copy
  1. /* ---------------------------------------------------------- 
  2. 文件名称:UsbEnumXML.cs 
  3.  
  4. 作者:秦建辉 
  5.  
  6. MSN:splashcn@msn.com 
  7. QQ:36748897 
  8.  
  9. 博客:http://blog.csdn.net/jhqin 
  10.  
  11. 开发环境: 
  12.     Visual Studio V2010 
  13.     .NET Framework 4 Client Profile 
  14.  
  15. 版本历史:     
  16.     V1.0    2011年10月28日 
  17.             将USB设备枚举信息导出为XML文档 
  18. ------------------------------------------------------------ */  
  19. using System;  
  20. using System.Collections.Generic;  
  21. using System.Xml.Linq;  
  22.   
  23. namespace Splash.IO.PORTS  
  24. {  
  25.     /// <summary>  
  26.     /// 将USB设备信息写入XML文件  
  27.     /// </summary>  
  28.     public partial class USB  
  29.     {  
  30.         /// <summary>  
  31.         /// 将USB设备枚举信息导出为XML文档  
  32.         /// </summary>  
  33.         /// <param name="xmlFileName">保存的XML文件名</param>  
  34.         /// <returns>  
  35.         ///     true:成功  
  36.         ///     false:失败  
  37.         /// </returns>  
  38.         public static Boolean EnumUsbToXML(String xmlFileName)  
  39.         {   // 创建根节点  
  40.             XElement RootNode = new XElement("Computer",  
  41.                 new XAttribute("MachineName", System.Environment.MachineName));  
  42.   
  43.             // 深度遍历主控制器  
  44.             HostControllerInfo[] HostControllersCollection = USB.AllHostControllers;  
  45.             if (HostControllersCollection != null)  
  46.             {  
  47.                 Int32 ControllerIndex = 1;  
  48.                 foreach (HostControllerInfo item in HostControllersCollection)  
  49.                 {   // 创建主控制器节点  
  50.                     String PNPDeviceID = item.PNPDeviceID;  
  51.                     String HcdDriverKeyName = USB.GetHcdDriverKeyName(PNPDeviceID);  
  52.                     XElement HostControllerNode = new XElement("HostController" + ControllerIndex,  
  53.                         new XAttribute("Name", item.Name),  // 设备名称  
  54.                         new XAttribute("PNPDeviceID", PNPDeviceID), // 设备ID  
  55.                         new XAttribute("HcdDriverKeyName", HcdDriverKeyName)    // 驱动键名  
  56.                         );  
  57.                     RootNode.Add(HostControllerNode);  
  58.                     ControllerIndex++;  
  59.   
  60.                     // 创建根集线器节点  
  61.                     String RootHubPath = USB.GetUsbRootHubPath(PNPDeviceID);  
  62.                     AddHubNode(HostControllerNode, RootHubPath, "RootHub");  
  63.                 }  
  64.             }  
  65.   
  66.             // 创建XML文档  
  67.             XDocument xmlTree = new XDocument(RootNode);  
  68.   
  69.             // 存储文件,序列化时对XML进行格式设置(缩进)  
  70.             xmlTree.Save(xmlFileName, SaveOptions.None);              
  71.             return true;  
  72.         }  
  73.           
  74.         /// <summary>  
  75.         /// 增加集线器节点  
  76.         /// </summary>  
  77.         /// <param name="ParentNode">父节点</param>  
  78.         /// <param name="HubPath">集线器路径</param>  
  79.         private static void AddHubNode(XElement ParentNode, String HubPath, String HubNodeName)  
  80.         {  
  81.             UsbNodeInformation[] NodeInfoCollection = USB.GetUsbNodeInformation(HubPath);  
  82.             if (NodeInfoCollection != null)  
  83.             {  
  84.                 USB_HUB_NODE NodeType = NodeInfoCollection[0].NodeType;  
  85.                 XElement HubNode = new XElement(HubNodeName,                  
  86.                     new XAttribute("Name", NodeInfoCollection[0].Name),  
  87.                     new XAttribute("PNPDeviceID", NodeInfoCollection[0].PNPDeviceID),  
  88.                     new XAttribute("Path", NodeInfoCollection[0].DevicePath),  
  89.                     new XAttribute("NodeType", NodeType)  
  90.                     );  
  91.   
  92.                 if (NodeType == USB_HUB_NODE.UsbHub)  
  93.                 {  
  94.                     Int32 NumberOfPorts = NodeInfoCollection[0].NumberOfPorts;  
  95.                     HubNode.Add(new XAttribute("NumberOfPorts", NumberOfPorts),  
  96.                         new XAttribute("HubIsBusPowered", NodeInfoCollection[0].HubIsBusPowered),                          
  97.                         new XAttribute("HubCharacteristics""0x" + NodeInfoCollection[0].HubCharacteristics.ToString("X4")),  
  98.                         new XAttribute("PowerOnToPowerGood", NodeInfoCollection[0].PowerOnToPowerGood),  
  99.                         new XAttribute("HubControlCurrent", NodeInfoCollection[0].HubControlCurrent)  
  100.                         );  
  101.   
  102.                     // 深度遍历端口  
  103.                     UsbNodeConnectionInformation[] NodeConnectionInfoCollection = USB.GetUsbNodeConnectionInformation(HubPath, NumberOfPorts);  
  104.                     if (NodeConnectionInfoCollection != null)  
  105.                     {  
  106.                         foreach (UsbNodeConnectionInformation NodeConnectionInfo in NodeConnectionInfoCollection)  
  107.                         {   // 增加端口节点  
  108.                             AddPortNode(HubNode, NodeConnectionInfo);  
  109.                         }  
  110.                     }  
  111.                 }  
  112.                 else  
  113.                 {  
  114.                     HubNode.Add("NumberOfInterfaces", NodeInfoCollection[0].NumberOfInterfaces);  
  115.                 }  
  116.   
  117.                 ParentNode.Add(HubNode);  
  118.             }  
  119.         }  
  120.   
  121.         /// <summary>  
  122.         /// 增加端口节点  
  123.         /// </summary>  
  124.         /// <param name="HubNode">集线器节点</param>  
  125.         /// <param name="NodeConnectionInfo">USB设备节点连接信息</param>  
  126.         private static void AddPortNode(XElement HubNode, UsbNodeConnectionInformation NodeConnectionInfo)  
  127.         {  
  128.             String DevicePath = NodeConnectionInfo.DevicePath;  
  129.             Int32 ConnectionIndex = NodeConnectionInfo.ConnectionIndex;  
  130.             USB_CONNECTION_STATUS ConnectionStatus = NodeConnectionInfo.ConnectionStatus;      
  131.   
  132.             // 创建端口节点  
  133.             XElement PortNode = new XElement("Port" + ConnectionIndex,  
  134.                 new XAttribute("DevicePath", DevicePath),  
  135.                 new XAttribute("ConnectionIndex", ConnectionIndex),  
  136.                 new XAttribute("ConnectionStatus", NodeConnectionInfo.ConnectionStatus)  
  137.                 );  
  138.   
  139.             if (ConnectionStatus == USB_CONNECTION_STATUS.DeviceConnected)  
  140.             {  
  141.                 Boolean DeviceIsHub = NodeConnectionInfo.DeviceIsHub;  
  142.                 PortNode.Add(new XAttribute("DeviceIsHub", DeviceIsHub),  
  143.                     new XAttribute("CurrentConfigurationValue", NodeConnectionInfo.CurrentConfigurationValue),  
  144.                     new XAttribute("Speed", NodeConnectionInfo.Speed),  
  145.                     new XAttribute("DeviceAddress", NodeConnectionInfo.DeviceAddress),  
  146.                     new XAttribute("NumberOfOpenPipes", NodeConnectionInfo.NumberOfOpenPipes)  
  147.                     );  
  148.   
  149.                 // 设备描述符信息  
  150.                 AddDeviceDescriptorNode(PortNode, ref NodeConnectionInfo.DeviceDescriptor);  
  151.   
  152.                 // 管道信息  
  153.                 AddPipeInfoNode(PortNode, ref NodeConnectionInfo.PipeList);  
  154.   
  155.                 // 外部集线器  
  156.                 if (DeviceIsHub)  
  157.                 {   // 获取外部Hub设备路径  
  158.                     String ExternalHubPath = GetExternalHubPath(DevicePath, ConnectionIndex);  
  159.   
  160.                     // 增加外部集线器节点  
  161.                     AddHubNode(PortNode, ExternalHubPath, "ExternalHub");  
  162.                 }  
  163.             }             
  164.   
  165.             HubNode.Add(PortNode);  
  166.         }  
  167.   
  168.         /// <summary>  
  169.         /// 增加设备描述符节点  
  170.         /// </summary>  
  171.         /// <param name="PortNode"></param>  
  172.         /// <param name="DeviceDescriptor"></param>  
  173.         private static void AddDeviceDescriptorNode(XElement PortNode, ref UsbDeviceDescriptor DeviceDescriptor)  
  174.         {  
  175.             XElement DeviceDescriptorNode = new XElement("DeviceDescriptor",  
  176.                 new XAttribute("bDescriptorType""0x" + DeviceDescriptor.bDescriptorType.ToString("X2")),  
  177.                 new XAttribute("UsbVersion", DeviceDescriptor.UsbVersion),  
  178.                 new XAttribute("bDeviceClass""0x" + DeviceDescriptor.bDeviceClass.ToString("X2")),  
  179.                 new XAttribute("bDeviceSubClass""0x" + DeviceDescriptor.bDeviceSubClass.ToString("X2")),  
  180.                 new XAttribute("bDeviceProtocol""0x" + DeviceDescriptor.bDeviceProtocol.ToString("X2")),  
  181.                 new XAttribute("bMaxPacketSize0", DeviceDescriptor.bMaxPacketSize0),    
  182.                 new XAttribute("bNumConfigurations", DeviceDescriptor.bNumConfigurations)  
  183.                 );  
  184.   
  185.             if (DeviceDescriptor.idVendor != 0)  
  186.             {  
  187.                 DeviceDescriptorNode.Add(new XAttribute("idVendor""0x" + DeviceDescriptor.idVendor.ToString("X4")));                  
  188.             }  
  189.   
  190.             if (DeviceDescriptor.idProduct != 0)  
  191.             {  
  192.                 DeviceDescriptorNode.Add(new XAttribute("idProduct""0x" + DeviceDescriptor.idProduct.ToString("X4")));  
  193.             }  
  194.   
  195.             if (!String.IsNullOrEmpty(DeviceDescriptor.DeviceVersion))  
  196.             {  
  197.                 DeviceDescriptorNode.Add(new XAttribute("DeviceVersion", DeviceDescriptor.DeviceVersion));  
  198.             }  
  199.   
  200.             if (!String.IsNullOrEmpty(DeviceDescriptor.Manufacturer))  
  201.             {  
  202.                 DeviceDescriptorNode.Add(new XAttribute("Manufacturer", DeviceDescriptor.Manufacturer));  
  203.             }  
  204.   
  205.             if (!String.IsNullOrEmpty(DeviceDescriptor.Product))  
  206.             {  
  207.                 DeviceDescriptorNode.Add(new XAttribute("Product", DeviceDescriptor.Product));  
  208.             }  
  209.   
  210.             if (!String.IsNullOrEmpty(DeviceDescriptor.SerialNumber))  
  211.             {  
  212.                 DeviceDescriptorNode.Add(new XAttribute("SerialNumber", DeviceDescriptor.SerialNumber));  
  213.             }  
  214.   
  215.             PortNode.Add(DeviceDescriptorNode);  
  216.         }  
  217.   
  218.         /// <summary>  
  219.         /// 增加管道信息节点  
  220.         /// </summary>  
  221.         /// <param name="PortNode">端口节点</param>  
  222.         /// <param name="PipeList">管道信息列表</param>  
  223.         private static void AddPipeInfoNode(XElement PortNode, ref List<UsbPipeInfo> PipeList)  
  224.         {  
  225.             if(PipeList != null)  
  226.             {  
  227.                 XElement PipeListNode = new XElement("PipeList");  
  228.                 Int32 PipeIndex = 1;  
  229.                 foreach(UsbPipeInfo item in PipeList)  
  230.                 {  
  231.                     XElement PipeInfoNode = new XElement("Pipe" + PipeIndex,  
  232.                         new XAttribute("ScheduleOffset", item.ScheduleOffset),  
  233.                         new XAttribute("bDescriptorType""0x" + item.bDescriptorType.ToString("X2")),  
  234.                         new XAttribute("bEndpointAddress""0x" + item.bEndpointAddress.ToString("X2")),  
  235.                         new XAttribute("bmAttributes""0x" + item.bmAttributes.ToString("X2")),  
  236.                         new XAttribute("wMaxPacketSize", item.wMaxPacketSize),  
  237.                         new XAttribute("bInterval", item.bInterval)  
  238.                         );  
  239.   
  240.                     PipeListNode.Add(PipeInfoNode);  
  241.                     PipeIndex++;  
  242.                 }  
  243.   
  244.                 PortNode.Add(PipeListNode);  
  245.             }   
  246.         }  
  247.     }  
  248. }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值