![](/img/trans.png)
[英]Get notification when a new drive is connected via USB or other means (C#)
[英]Receive notification in C# app when a new USB audio device is connected or disconnected?
我有一個在Windows 8.1或更高版本上運行的C#Windows Forms應用程序,並且可以進行語音識別。 當新的USB音頻輸入設備連接到系統時,我想收到通知。 我希望Windows API中有一個通知服務,該服務將告訴我何時音頻設備連接到系統或與系統斷開連接。
是否有這樣的通知,還是當我檢測到更改時是否不斷重新掃描可用的音頻輸入設備並創建自己的通知? 我顯然不想重新發明輪子。
以下內容將以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)
{
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.