USB设备插拔监听
需要重写WndProc方法,该方法可以监听系统的一些事件,我试了一下,插拔U盘有响应,但是插拔USB麦克风只会触发设备节点改变的方法
//事件代码
private const int WM_DEVICECHANGE = 0x219; //设备改变
private const int DBT_DEVICEARRIVAL = 0x8000; //检测到新设备
private const int DBT_DEVICEREMOVECOMPLETE = 0x8004; //移除设备
private const int DBT_DEVNODES_CHANGED = 0x0007; //设备节点改变
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);//调用父类方法,以确保其他功能正常
//Console.WriteLine("***************************"+ (int)m.WParam);
switch (m.Msg)
{
case WM_DEVICECHANGE://设备改变事件
switch ((int)m.WParam)
{
case DBT_DEVICEARRIVAL:
Console.WriteLine("新设备");
int devType = Marshal.ReadInt32(m.LParam, 4);
Console.WriteLine("设备类型:" + devType);
break;
case DBT_DEVICEREMOVECOMPLETE:
Console.WriteLine("设备移除");
break;
case DBT_DEVNODES_CHANGED:
Console.WriteLine("设备节点改变");
break;
}
break;
}
}
USB信息查询
下面是各种和USB相关的信息查询,我想实现USB插在哪个口,但是下面的信息之间毫无关联,只能通过监听USB插拔遍历总线来确定插在哪个总线上
可查询所有在用的USB总线Num:select * from Win32_Bus where DeviceID like 'PNP_BUS%'
可遍历所有的USB口:select * from Win32_USBControllerDevice
可遍历所有的即插即用设备:SELECT * FROM Win32_PnPEntity
可遍历Hub设备(Hub设备和即插即用设备不同,USB麦克风就是即插即用设备,U盘就是HUB设备):Select * From Win32_USBHub
可遍历音频设备:SELECT * FROM Win32_SoundDevice
private void getDevicesInfo()
{
///即插即用总线信息/
ManagementObjectSearcher mos0 = new ManagementObjectSearcher(@"select * from Win32_Bus where DeviceID like 'PNP_BUS%'");
foreach (ManagementObject mo in mos0.Get())
{
foreach (PropertyData p in mo.Properties)
{
Console.WriteLine(p.Name + "--------" + p.Value);
}
Console.WriteLine("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
}
/USB控制器以及连接的设备/
ManagementObjectSearcher moc1 = new ManagementObjectSearcher(@"select * from Win32_USBController where Manufacturer='Intel'");
foreach (ManagementObject mo1 in moc1.Get())
{
foreach (PropertyData p in mo1.Properties)
{
Console.WriteLine(p.Name + "--------" + p.Value);
}
Console.WriteLine("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
}
//USB控制器以及当前与他们相关联的设备的集合//
ManagementObjectSearcher mos1 = new ManagementObjectSearcher(@"select * from Win32_USBControllerDevice");
foreach (ManagementObject mo1 in mos1.Get())
{
foreach (PropertyData p in mo1.Properties)
{
Console.WriteLine(p.Name + "--------" + p.Value);
}
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
即插即用的某一个USB设备//
ManagementObjectSearcher mos = new ManagementObjectSearcher(@"select * from Win32_PnPEntity where DeviceID like '%usb%mi%' and (Manufacturer like 'microsoft' or Service='usbvideo')");
foreach (ManagementObject mo in mos.Get())
{
foreach (PropertyData p in mo.Properties)
{
if ((p.Name == "Caption") || (p.Name == "ClassGuid") || (p.Name == "DeviceID") || (p.Name == "PNPDeviceID"))
{
Console.WriteLine(p.Name + "--------" + p.Value);
Console.WriteLine( "********************");
}
}
}
某一个USBHUB谁被//
ManagementObjectSearcher moc2 = new ManagementObjectSearcher(@"Select * From Win32_USBHub");
foreach (ManagementObject mo2 in moc2.Get())
{
foreach (PropertyData p in mo2.Properties)
{
Console.WriteLine(p.Name + "--------" + p.Value);
}
Console.WriteLine("+++++++++++++++++++++++++++++++++");
}
String Dependent = @"SWD\\MMDEVAPI\\{0.0.1.00000000}.{9A44C4D0-0BF5-4272-95FF-741DF041E4BD}";
//SELECT * FROM Win32_PnPEntity WHERE DeviceID=" + Dependent
ManagementObjectSearcher moc3 = new ManagementObjectSearcher(@"SELECT * FROM Win32_PnPEntity WHERE DeviceID like '%0.0.1.00000000%'");
foreach (ManagementObject mo3 in moc3.Get())
{
foreach (PropertyData p in mo3.Properties)
{
Console.WriteLine(p.Name + "--------" + p.Value);
}
Console.WriteLine("=====================================");
}
ManagementObjectSearcher moc4 = new ManagementObjectSearcher(@"SELECT * FROM Win32_SoundDevice");
foreach (ManagementObject mo4 in moc4.Get())
{
foreach (PropertyData p in mo4.Properties)
{
Console.WriteLine(p.Name + "--------" + p.Value);
}
Console.WriteLine("#######################################");
}
}