当连接或断开新的 USB 音频设备时,在 C# 应用程序中接收通知?

Receive notification in C# app when a new USB audio device is connected or disconnected?

我有一个 C# Windows Forms 应用程序可以在 Windows 8.1 或更高版本上运行并且可以进行语音认出。我想在新的 USB 音频输入设备连接到系统时收到通知。我希望 Windows API 中有一个通知服务,它会告诉我音频设备何时连接到系统 与系统断开连接。

是否有这样的通知可用,或者我是否不断重新扫描可用的音频输入设备并在检测到变化时创建自己的通知?我显然不想重新发明轮子。

以下将以 2 秒间隔的扫描速率侦听 USB plug-in/turn-on un-plug/turn-off

        //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)
        {
        }