c#中的__InstanceModificationEvent类

时间:2018-06-13 21:46:32

标签: c# wmi

我正在尝试在WMI查询中使用__InstanceModificationEvent类,以便当usb的大小发生变化时,此事件将启动。但是,这不会执行任何操作。能否请您提供有关如何使用__InstanceModificationEvent类的提示?

SELECT * 
FROM __InstanceModificationEvent 
WITHIN 2 
WHERE TargetInstance ISA 'Win32_Diskdrive' 
    and TargetInstance.Interfacetype = 'USB'

1 个答案:

答案 0 :(得分:0)

您可以使用ORMi轻松创建观察者。

1)定义你的班级:

[WMIClass("__InstanceModificationEvent")]
public class ModificationEvent
{
    public string TargetInstance { get; set; }

    [WMIProperty("TIME_CREATED")]
    public long Time { get; set; }
}

2)然后创建观察者并开始处理事件:

WMIWatcher watcher = new WMIWatcher("root\\CimV2", "SELECT * FROM __InstanceModificationEvent WHERE TargetInstance ISA 'Win32_Diskdrive' and TargetInstance.Interfacetype = 'USB'", typeof(ModificationEvent));
watcher.WMIEventArrived += Watcher_WMIEventArrived;

private static void Watcher_WMIEventArrived(object sender, WMIEventArgs e)
{
    ModificationEvent yourEvent = (ModificationEvent)e.Object;

    Console.Log(yourEvent.TargetInstance);
}