C#:USB设备枚举(三)输出枚举信息到XML文档
/* ----------------------------------------------------------
文件名称:UsbEnumXML.cs
作者:秦建辉
MSN:splashcn@msn.com
QQ:36748897
博客:http://blog.csdn.net/jhqin
开发环境:
Visual Studio V2010
.NET Framework 4 Client Profile
版本历史:
V1.0 2011年10月28日
将USB设备枚举信息导出为XML文档
------------------------------------------------------------ */
using System;
using System.Collections.Generic;
using System.Xml.Linq;
namespace Splash.IO.PORTS
{
/// <summary>
/// 将USB设备信息写入XML文件
/// </summary>
public partial class USB
{
/// <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)
{ // 创建主控制器节点
String PNPDeviceID = item.PNPDeviceID;
String HcdDriverKeyName = USB.GetHcdDriverKeyName(PNPDeviceID);
XElement HostControllerNode = new XElement("HostController" + ControllerIndex,
new XAttribute("Name", item.Name), // 设备名称
new XAttribute("PNPDeviceID", PNPDeviceID), // 设备ID
new XAttribute("HcdDriverKeyName", HcdDriverKeyName) // 驱动键名
);
RootNode.Add(HostControllerNode);
ControllerIndex++;
// 创建根集线器节点
String RootHubPath = USB.GetUsbRootHubPath(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
补充:软件开发 , C# ,