c#使用wmi查询usb设备信息示例
开发环境:visual studio v2010 .net framework 4 client profile
using system;
using system.management;
using system.text.regularexpressions;
using system.collections.generic;
namespace splash.io.ports
{
/// <summary>
/// 即插即用设备信息结构
/// </summary>
public struct pnpentityinfo
{
public string pnpdeviceid; // 设备id
public string name; // 设备名称
public string description; // 设备描述
public string service; // 服务
public string status; // 设备状态
public uint16 vendorid; // 供应商标识
public uint16 productid;// 产品编号
public guid classguid; // 设备安装类guid
}
/// <summary>
/// 基于wmi获取usb设备信息
/// </summary>
public partial class usb
{
#region usbdevice
/// <summary>
/// 获取所有的usb设备实体(过滤没有vid和pid的设备)
/// </summary>
public static pnpentityinfo[] allusbdevices
{
get
{
return whousbdevice(uint16.minvalue, uint16.minvalue, guid.empty);
}
}
/// <summary>
/// 查询usb设备实体(设备要求有vid和pid)
/// </summary>
/// <param name="vendorid">供应商标识,minvalue忽视</param>
/// <param name="productid">产品编号,minvalue忽视</param>
/// <param name="classguid">设备安装类guid,empty忽视</param>
/// <returns>设备列表</returns>
public static pnpentityinfo[] whousbdevice(uint16 vendorid, uint16 productid, guid classguid)
{
list<pnpentityinfo> usbdevices = new list<pnpentityinfo>();
// 获取usb控制器及其相关联的设备实体
managementobjectcollection usbcontrollerdevicecollection = new managementobjectsearcher("select * from win32_usbcontrollerdevice").get();
if (usbcontrollerdevicecollection != null)
{
foreach (managementobject usbcontrollerdevice in usbcontrollerdevicecollection)
{ // 获取设备实体的deviceid
string dependent = (usbcontrollerdevice["dependent"] as string).split(new char[] { '=' })[1];
// 过滤掉没有vid和pid的usb设备
match match = regex.match(dependent, "vid_[0-9|a-f]{4}&pid_[0-9|a-f]{4}");
if (match.success)
{
uint16 thevendorid = convert.touint16(match.value.substring(4, 4), 16); // 供应商标识
if (vendorid != uint16.minvalue && vendorid != thevendorid) continue;
uint16 theproductid = convert.touint16(match.value.substring(13, 4), 16); // 产品编号
if (productid != uint16.minvalue && productid != theproductid) continue;
managementobjectcollection pnpentitycollection = new managementobjectsearcher("select * from win32_pnpentity where deviceid=" + dependent).get();
if (pnpentitycollection != null)
{
foreach (managementobject entity in pnpentitycollection)
{
guid theclassguid = new guid(entity["classguid"] as string);// 设备安装类guid
if (classguid != guid.empty && classguid != theclassguid) continue;
pnpentityinfo element;
element.pnpdeviceid = entity["pnpdeviceid"] as string; // 设备id
element.name = entity["name"] as string;// 设备名称
element.description = entity["description"] as string; // 设备描述
element.service = entity["service"] as string; // 服务
element.status = entity["status"] as string;// 设备状态
element.vendorid = thevendorid; // 供应商标识
element.productid = theproductid; // 产品编号
element.classguid = theclassguid; // 设备安装类guid
usbdevices.add(element);
}
}
}
}
}
if (usbdevices.count == 0) return null; else return usbdevices.toarray();
}
/// <summary>
/// 查询usb设备实体(设备要求有vid和pid)
/// </summary>
/// <param name="vendorid">供应商标识,minvalue忽视</param>
/// <param name="productid">产品编号,minvalue忽视</param>
/// <returns>设备列表</returns>
public static pnpentityinfo[] whousbdevice(uint16 vendorid, uint16 productid)
{
return whousbdevice(vendorid, productid, guid.empty);
}
/// <summary>
/// 查询usb设备实体(设备要求有vid和pid)
/// </summary>
/// <param name="classguid">设备安装类guid,empty忽视</param>
/// <returns>设备列表</returns>
public static pnpentityinfo[] whousbdevice(guid classguid)
{
return whousbdevice(uint16.minvalue, uint16.minvalue, classguid);
}
/// <summary>
/// 查询usb设备实体(设备要求有vid和pid)
/// </summary>
/// <param name="pnpdeviceid">设备id,可以是不完整信息</param>
/// <returns>设备列表</returns>
public static pnpentityinfo[] whousbdevice(string pnpdeviceid)
{
list<pnpentityinfo> usbdevices = new list<pnpentityinfo>();
// 获取usb控制器及其相关联的设备实体
managementobjectcollection usbcontrollerdevicecollection = new managementobjectsearcher("select * from win32_usbcontrollerdevice").get();
if (usbcontrollerdevicecollection != null)
{
foreach (managementobject usbcontrollerdevice in usbcontrollerdevicecollection)
{ // 获取设备实体的deviceid
string dependent = (usbcontrollerdevice["dependent"] as string).split(new char[] { '=' })[1];
if (!string.isnullorempty(pnpdeviceid))
{ // 注意:忽视大小写
if (dependent.indexof(pnpdeviceid, 1, pnpdeviceid.length - 2, stringcomparison.ordinalignorecase) == -1) continue;
}
// 过滤掉没有vid和pid的usb设备
match match = regex.match(dependent, "vid_[0-9|a-f]{4}&pid_[0-9|a-f]{4}");
if (match.success)
{
managementobjectcollection pnpentitycollection = new managementobjectsearcher("select * from win32_pnpentity where deviceid=" + dependent).get();
if (pnpentitycollection != null)
{
foreach (managementobject entity in pnpentitycollection)
{
pnpentityinfo element;
element.pnpdeviceid = entity["pnpdeviceid"] as string; // 设备id
element.name = entity["name"] as string;// 设备名称
element.description = entity["description"] as string; // 设备描述
element.service = entity["service"] as string; // 服务
element.status = entity["status"] as string;// 设备状态
element.vendorid = convert.touint16(match.value.substring(4, 4), 16); // 供应商标识
element.productid = convert.touint16(match.value.substring(13, 4), 16); // 产品编号 // 产品编号
element.classguid = new guid(entity["classguid"] as string);// 设备安装类guid
usbdevices.add(element);
}
}
}
}
}
if (usbdevices.count == 0) return null; else return usbdevices.toarray();
}
/// <summary>
/// 根据服务定位usb设备
/// </summary>
/// <param name="servicecollection">要查询的服务集合</param>
/// <returns>设备列表</returns>
public static pnpentityinfo[] whousbdevice(string[] servicecollection)
{
if (servicecollection == null || servicecollection.length == 0)
return whousbdevice(uint16.minvalue, uint16.minvalue, guid.empty);
list<pnpentityinfo> usbdevices = new list<pnpentityinfo>();
// 获取usb控制器及其相关联的设备实体
managementobjectcollection usbcontrollerdevicecollection = new managementobjectsearcher("select * from win32_usbcontrollerdevice").get();
if (usbcontrollerdevicecollection != null)
{
foreach (managementobject usbcontrollerdevice in usbcontrollerdevicecollection)
{ // 获取设备实体的deviceid
string dependent = (usbcontrollerdevice["dependent"] as string).split(new char[] { '=' })[1];
// 过滤掉没有vid和pid的usb设备
match match = regex.match(dependent, "vid_[0-9|a-f]{4}&pid_[0-9|a-f]{4}");
if (match.success)
{
managementobjectcollection pnpentitycollection = new managementobjectsearcher("select * from win32_pnpentity where deviceid=" + dependent).get();
if (pnpentitycollection != null)
{
foreach (managementobject entity in pnpentitycollection)
{
string theservice = entity["service"] as string; // 服务
if (string.isnullorempty(theservice)) continue;
foreach (string service in servicecollection)
{ // 注意:忽视大小写
if (string.compare(theservice, service, true) != 0) continue;
pnpentityinfo element;
element.pnpdeviceid = entity["pnpdeviceid"] as string; // 设备id
element.name = entity["name"] as string;// 设备名称
element.description = entity["description"] as string; // 设备描述
element.service = theservice; // 服务
element.status = entity["status"] as string;// 设备状态
element.vendorid = convert.touint16(match.value.substring(4, 4), 16); // 供应商标识
element.productid = convert.touint16(match.value.substring(13, 4), 16); // 产品编号
element.classguid = new guid(entity["classguid"] as string);// 设备安装类guid
usbdevices.add(element);
break;
}
}
}
}
}
}
if (usbdevices.count == 0) return null; else return usbdevices.toarray();
}
#endregion
#region pnpentity
/// <summary>
/// 所有即插即用设备实体(过滤没有vid和pid的设备)
/// </summary>
public static pnpentityinfo[] allpnpentities
{
get
{
return whopnpentity(uint16.minvalue, uint16.minvalue, guid.empty);
}
}
/// <summary>
/// 根据vid和pid及设备安装类guid定位即插即用设备实体
/// </summary>
/// <param name="vendorid">供应商标识,minvalue忽视</param>
/// <param name="productid">产品编号,minvalue忽视</param>
/// <param name="classguid">设备安装类guid,empty忽视</param>
/// <returns>设备列表</returns>
/// <remarks>
/// hid:{745a17a0-74d3-11d0-b6fe-00a0c90f57da}
/// imaging device:{6bdd1fc6-810f-11d0-bec7-08002be2092f}
/// keyboard:{4d36e96b-e325-11ce-bfc1-08002be10318}
/// mouse:{4d36e96f-e325-11ce-bfc1-08002be10318}
/// network adapter:{4d36e972-e325-11ce-bfc1-08002be10318}
/// usb:{36fc9e60-c465-11cf-8056-444553540000}
/// </remarks>
public static pnpentityinfo[] whopnpentity(uint16 vendorid, uint16 productid, guid classguid)
{
list<pnpentityinfo> pnpentities = new list<pnpentityinfo>();
// 枚举即插即用设备实体
string vidpid;
if (vendorid == uint16.minvalue)
{
if (productid == uint16.minvalue)
vidpid = "'%vid[_]____&pid[_]____%'";
else
vidpid = "'%vid[_]____&pid[_]" + productid.tostring("x4") + "%'";
}
else
{
if (productid == uint16.minvalue)
vidpid = "'%vid[_]" + vendorid.tostring("x4") + "&pid[_]____%'";
else
vidpid = "'%vid[_]" + vendorid.tostring("x4") + "&pid[_]" + productid.tostring("x4") + "%'";
}
string querystring;
if (classguid == guid.empty)
querystring = "select * from win32_pnpentity where pnpdeviceid like" + vidpid;
else
querystring = "select * from win32_pnpentity where pnpdeviceid like" + vidpid + " and classguid='" + classguid.tostring("b") + "'";
managementobjectcollection pnpentitycollection = new managementobjectsearcher(querystring).get();
if (pnpentitycollection != null)
{
foreach (managementobject entity in pnpentitycollection)
{
string pnpdeviceid = entity["pnpdeviceid"] as string;
match match = regex.match(pnpdeviceid, "vid_[0-9|a-f]{4}&pid_[0-9|a-f]{4}");
if (match.success)
{
pnpentityinfo element;
element.pnpdeviceid = pnpdeviceid; // 设备id
element.name = entity["name"] as string;// 设备名称
element.description = entity["description"] as string; // 设备描述
element.service = entity["service"] as string; // 服务
element.status = entity["status"] as string;// 设备状态
element.vendorid = convert.touint16(match.value.substring(4, 4), 16); // 供应商标识
element.productid = convert.touint16(match.value.substring(13, 4), 16); // 产品编号
element.classguid = new guid(entity["classguid"] as string);// 设备安装类guid
pnpentities.add(element);
}
}
}
if (pnpentities.count == 0) return null; else return pnpentities.toarray();
}
/// <summary>
/// 根据vid和pid定位即插即用设备实体
/// </summary>
/// <param name="vendorid">供应商标识,minvalue忽视</param>
/// <param name="productid">产品编号,minvalue忽视</param>
/// <returns>设备列表</returns>
public static pnpentityinfo[] whopnpentity(uint16 vendorid, uint16 productid)
{
return whopnpentity(vendorid, productid, guid.empty);
}
/// <summary>
/// 根据设备安装类guid定位即插即用设备实体
/// </summary>
/// <param name="classguid">设备安装类guid,empty忽视</param>
/// <returns>设备列表</returns>
public static pnpentityinfo[] whopnpentity(guid classguid)
{
return whopnpentity(uint16.minvalue, uint16.minvalue, classguid);
}
/// <summary>
/// 根据设备id定位设备
/// </summary>
/// <param name="pnpdeviceid">设备id,可以是不完整信息</param>
/// <returns>设备列表</returns>
/// <remarks>
/// 注意:对于下划线,需要写成“[_]”,否则视为任意字符
/// </remarks>
public static pnpentityinfo[] whopnpentity(string pnpdeviceid)
{
list<pnpentityinfo> pnpentities = new list<pnpentityinfo>();
// 枚举即插即用设备实体
string querystring;
if (string.isnullorempty(pnpdeviceid))
{
querystring = "select * from win32_pnpentity where pnpdeviceid like '%vid[_]____&pid[_]____%'";
}
else
{ // like子句中有反斜杠字符将会引发wql查询异常
querystring = "select * from win32_pnpentity where pnpdeviceid like '%" + pnpdeviceid.replace('\\', '_') + "%'";
}
managementobjectcollection pnpentitycollection = new managementobjectsearcher(querystring).get();
if (pnpentitycollection != null)
{
foreach (managementobject entity in pnpentitycollection)
{
string thepnpdeviceid = entity["pnpdeviceid"] as string;
match match = regex.match(thepnpdeviceid, "vid_[0-9|a-f]{4}&pid_[0-9|a-f]{4}");
if (match.success)
{
pnpentityinfo element;
element.pnpdeviceid = thepnpdeviceid; // 设备id
element.name = entity["name"] as string;// 设备名称
element.description = entity["description"] as string; // 设备描述
element.service = entity["service"] as string; // 服务
element.status = entity["status"] as string;// 设备状态
element.vendorid = convert.touint16(match.value.substring(4, 4), 16); // 供应商标识
element.productid = convert.touint16(match.value.substring(13, 4), 16); // 产品编号
element.classguid = new guid(entity["classguid"] as string);// 设备安装类guid
pnpentities.add(element);
}
}
}
if (pnpentities.count == 0) return null; else return pnpentities.toarray();
}
/// <summary>
/// 根据服务定位设备
/// </summary>
/// <param name="servicecollection">要查询的服务集合,null忽视</param>
/// <returns>设备列表</returns>
/// <remarks>
/// 跟服务相关的类:
/// win32_systemdriverpnpentity
/// win32_systemdriver
/// </remarks>
public static pnpentityinfo[] whopnpentity(string[] servicecollection)
{
if (servicecollection == null || servicecollection.length == 0)
return whopnpentity(uint16.minvalue, uint16.minvalue, guid.empty);
list<pnpentityinfo> pnpentities = new list<pnpentityinfo>();
// 枚举即插即用设备实体
string querystring = "select * from win32_pnpentity where pnpdeviceid like '%vid[_]____&pid[_]____%'";
managementobjectcollection pnpentitycollection = new managementobjectsearcher(querystring).get();
if (pnpentitycollection != null)
{
foreach (managementobject entity in pnpentitycollection)
{
string pnpdeviceid = entity["pnpdeviceid"] as string;
match match = regex.match(pnpdeviceid, "vid_[0-9|a-f]{4}&pid_[0-9|a-f]{4}");
if (match.success)
{
string theservice = entity["service"] as string;// 服务
if (string.isnullorempty(theservice)) continue;
foreach (string service in servicecollection)
{ // 注意:忽视大小写
if (string.compare(theservice, service, true) != 0) continue;
pnpentityinfo element;
element.pnpdeviceid = pnpdeviceid; // 设备id
element.name = entity["name"] as string;// 设备名称
element.description = entity["description"] as string; // 设备描述
element.service = theservice; // 服务
element.status = entity["status"] as string;// 设备状态
element.vendorid = convert.touint16(match.value.substring(4, 4), 16); // 供应商标识
element.productid = convert.touint16(match.value.substring(13, 4), 16); // 产品编号
element.classguid = new guid(entity["classguid"] as string);// 设备安装类guid
pnpentities.add(element);
break;
}
}
}
}
if (pnpentities.count == 0) return null; else return pnpentities.toarray();
}
#endregion
}
}
推荐阅读