我有一个 C#Windows窗体应用程序,可在 Windows 8.1 或更新版本上运行并进行语音识别。我想在新的USB音频输入设备连接到系统时收到通知。我希望Windows API中有一个通知服务,告诉我何时和音频设备连接到系统或的系统。
是否有这样的通知,或者我是否经常重新扫描可用的音频输入设备并在检测到更改时创建自己的通知?我显然不想重新发明轮子。
答案 0 :(得分:1)
以下将以2秒间隔的扫描速率监听USB插件/开启非插拔/关闭
//turn on USB device event
WqlEventQuery q_creation = new WqlEventQuery();
q_creation.EventClassName = "__InstanceCreationEvent";
q_creation.WithinInterval = new TimeSpan(0, 0, 2);
q_creation.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'";
mwe_creation = new ManagementEventWatcher(q_creation);
mwe_creation.EventArrived += new EventArrivedEventHandler(USBEventArrived_Creation);
mwe_creation.Start();
//turn off USB device event
WqlEventQuery q_deletion = new WqlEventQuery();
q_deletion.EventClassName = "__InstanceDeletionEvent";
q_deletion.WithinInterval = new TimeSpan(0, 0, 2);
q_deletion.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice' ";
mwe_deletion = new ManagementEventWatcher(q_deletion);
mwe_deletion.EventArrived += new EventArrivedEventHandler(USBEventArrived_Deletion);
mwe_deletion.Start();
private void USBEventArrived_Creation(object sender, EventArrivedEventArgs e)
{
}
private void USBEventArrived_Deletion(object sender, EventArrivedEventArgs e)
{
}