15

我想检测用户何时插入或移除 USB 声卡。当这种情况发生时,我已经设法真正捕捉到事件,但我不知道刚刚插入了什么。

我尝试了一种基于这个问题的方法:

string query =
    "SELECT * FROM __InstanceCreationEvent " +
    "WITHIN 2 "
  + "WHERE TargetInstance ISA 'Win32_PnPEntity'";
var watcher = new ManagementEventWatcher(query);
watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
watcher.Start();

虽然我通过 EventArrived 事件获得通知,但我不知道如何确定刚刚插入的设备的实际名称。我已经检查了每个属性,但无法从中得出正面或反面。

我还尝试了一个不同的查询:

var query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent where EventType = 1 or EventType = 2");
var watcher = new ManagementEventWatcher(query);
watcher.EventArrived += watcher_EventArrived;
watcher.Stopped += watcher_Stopped;
watcher.Query = query;
watcher.Start();

但也无济于事。有没有办法找到插入或移除的设备的名称。

最重要的是,我想知道 USB 声卡何时插入或从系统中移除。它应该可以在 Windows 7 和 Vista 上运行(尽管我只会满足于 Win7)。

编辑:根据获胜提交者的建议,我创建了一个包含所有功能的完整解决方案。

4

3 回答 3

12

如果我使用您的第一个代码,我可以这样定义我的事件:

    // define USB class guid (from devguid.h)
    static readonly Guid GUID_DEVCLASS_USB = new Guid("{36fc9e60-c465-11cf-8056-444553540000}");

    static void watcher_EventArrived(object sender, EventArrivedEventArgs e)
    {
        ManagementBaseObject instance = (ManagementBaseObject )e.NewEvent["TargetInstance"];
        if (new Guid((string)instance["ClassGuid"]) == GUID_DEVCLASS_USB)
        {
            // we're only interested by USB devices, dump all props
            foreach (var property in instance.Properties)
            {
                Console.WriteLine(property.Name + " = " + property.Value);
            }
        }
    }

这将转储如下内容:

Availability =
Caption = USB Mass Storage Device
ClassGuid = {36fc9e60-c465-11cf-8056-444553540000}
CompatibleID = System.String[]
ConfigManagerErrorCode = 0
ConfigManagerUserConfig = False
CreationClassName = Win32_PnPEntity
Description = USB Mass Storage Device
DeviceID = USB\VID_18A5&PID_0243\07072BE66DD78609
ErrorCleared =
ErrorDescription =
HardwareID = System.String[]
InstallDate =
LastErrorCode =
Manufacturer = Compatible USB storage device
Name = USB Mass Storage Device
PNPDeviceID = USB\VID_18A5&PID_0243\07072BE66DD78609
PowerManagementCapabilities =
PowerManagementSupported =
Service = USBSTOR
Status = OK
StatusInfo =
SystemCreationClassName = Win32_ComputerSystem
SystemName = KILROY_WAS_HERE

这应该包含您需要的所有内容,包括您可以使用类似instance["DeviceID"].

于 2013-05-28T13:31:39.680 回答
3

编辑 1:哦,看到它不是 USB 存储设备,而只是一个 USB 设备。我会寻找另一种解决方案。


描述相同问题的两个链接:

http://hintdesk.com/c-catch-usb-plug-and-unplug-event/

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/37123526-83fa-4e96-a767-715fe225bf28/

if (e.NewEvent.ClassPath.ClassName == "__InstanceCreationEvent")
{
    Console.WriteLine("USB was plugged in");
    //Get disk letter
    foreach (ManagementObject partition in new ManagementObjectSearcher(
"ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + mbo.Properties["DeviceID"].Value
+ "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition").Get())
    {
        foreach (ManagementObject disk in new ManagementObjectSearcher(
                    "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='"
                        + partition["DeviceID"]
                        + "'} WHERE AssocClass = Win32_LogicalDiskToPartition").Get())
        {
            Console.WriteLine("Disk=" + disk["Name"]);
        }
    }
}
于 2013-05-18T08:49:46.653 回答
2

当我尝试@AngryHacker 解决方案时,我注意到DeviceChangedEventArgs该类从未被调用过。我删除了它,只是添加Console.WriteLines()watcher_eventArrived方法。

除了删除DeviceChangedEventArgs,这是我的更改:

 (at line 46 in EstablishedWatchEvents)
 // setup the query to monitor removal
const string qryRemoval = "SELECT *" +  "FROM __InstanceDeletionEvent "
             + "WITHIN 2 " + "WHERE TargetInstance ISA 'Win32_PnPEntity' ";

 #region Events

 private void insertWatcher_EventArrived(object sender, EventArrivedEventArgs e)
 {

     var mbo = (ManagementBaseObject) e.NewEvent["TargetInstance"];
     if (new Guid((string) mbo["ClassGuid"]) == GUID_DEVCLASS_USB)
     {
         var deviceName = (string) mbo["Name"];
         Console.WriteLine(deviceName + " was inserted");

     }
 }

 private void removeWatcher_EventArrived(object sender, EventArrivedEventArgs e)
 {

     var mbo = (ManagementBaseObject)e.NewEvent["TargetInstance"];

     if (new Guid((string)mbo["ClassGuid"]) == GUID_DEVCLASS_USB)
     {
         var deviceName = (string)mbo["Name"];
         Console.WriteLine(deviceName + " was removed");
     }
 }

 #endregion
于 2014-05-29T17:00:35.960 回答