0

I have installed WMI code create creator from here and I am wondering what namespace and classes we need to select from application as shown below in an image in order to get an automatic call back in WMI when things changes on computer.

enter image description here After getting the right namespace and class, I will generate a C# code from the code language on the menu (as shown above in an image) at the top from the code creator and run it on my computer

1 Answers1

0

You can use WMI Events for this purpose, here are some references:

https://msdn.microsoft.com/en-us/library/aa393013%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396

https://www.pluralsight.com/blog/it-ops/monitor-folders-with-wmi-event-subscriptions

It would look something like;

  WmiQuery ="Select * From SomeWMIEvent";    
  Watcher = new ManagementEventWatcher(Scope, new EventQuery(WmiQuery));
  Watcher.EventArrived += new EventArrivedEventHandler(this.WmiEventHandler);
Milney
  • 6,253
  • 2
  • 19
  • 33
  • thanks. It did help. I am wondering what namespace and class I need to select from the WMI code creator ? –  Aug 18 '17 at 14:03
  • 1
    ...well it depends what you need to monitor? Which things do you want notified of change on? – Milney Aug 18 '17 at 14:06
  • Sorry I am a beginner in WMI so that's the only way I know. `I go into the WMI code creator and then I select a specific namespace and class. After selection, I generate a C# code and run it on my computer.` Any kind of event, like plugging in of USB device, plugging out of USB device, any new installation, any changes in the windows directory and so on. –  Aug 18 '17 at 14:09
  • Well for USB you can either use Win32_VolumeChangeEvent WHERE EventType = 2 or __InstanceDeletionEvent WHERE TargetInstance IS A 'Win32_USBHub' – Milney Aug 18 '17 at 14:11
  • Just lookup the WMI documentation, and filter events. It tells you what they all do... – Milney Aug 18 '17 at 14:11