【发布时间】:2015-09-08 12:50:26
【问题描述】:
USB thumb drives
USB harddisks
USB DVD writer
USB Bluetooth devices
USB headsets
usb mouse
USB keyboard
USB webcams / cameras
只想使用事件处理程序检测任何类型的 USB 设备... 将不胜感激任何帮助...
WqlEventQuery q_creation = new WqlEventQuery();
private void Form2_Load(object sender, EventArgs e)
{
q_creation.EventClassName = "__InstanceCreationEvent";
q_creation.WithinInterval = new TimeSpan(0, 0, 2); //How often do you want to check it? 2Sec.
q_creation.Condition = @"TargetInstance ISA 'Win32_DiskDriveToDiskPartition'";
var mwe_creation = new ManagementEventWatcher(q_creation);
mwe_creation.EventArrived += new EventArrivedEventHandler(USBEventArrived_Creation);
mwe_creation.Start(); // Start listen for events
}
/// <summary>
/// Callback event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
internal void USBEventArrived_Creation(object sender, EventArrivedEventArgs e)
{
MessageBox.Show("Device Connected");
}
这是我尝试过的代码。
【问题讨论】:
-
这只检测笔式驱动器。不检测鼠标和其他设备
-
但这对于您的问题来说是一个很好的起点,不是吗?这意味着您需要花一些时间在谷歌上搜索 WMI,使用它您可以访问任何内容。向我们表明您已经尝试过某些事情并且不会懒得去寻找答案
-
其实我在google里搜过也试过了,我的代码也只检测pendrive。
-
可能是this 会有所帮助。它帮助了我。
标签: c# winforms callback usb managementeventwatcher