Consuming WMI Events
I am trying to trigger a script when a user drops a file into a specific folder. I am not talking about a drop event within an application, I mean responding to the event at windows level.
To do this I need to write a class which consumes the windows event. This would probably need to be written as a service. The following code detects when a windows service changes, but I cannot find any info on how to respond to the folder changed event.
Any info in this area would be greatly appreciated.
-------------
using System;
using System.Management;
public class EventWatcherPolling
{
public static int Main(string[] args)
{
// Create event query to be notified within 1 second of
// a change in a service
WqlEventQuery query = new WqlEventQuery("__InstanceModificationEvent", new TimeSpan(0,0,1), "TargetInstance isa \"Win32_Service\"");
// Initialize an event watcher and subscribe to events
// that match this query
ManagementEventWatcher watcher = new ManagementEventWatcher(query);
// Block until the next event occurs
// Note: this can be done in a loop if waiting for
// more than one occurrence
ManagementBaseObject e = watcher.WaitForNextEvent();
//Display information from the event
Console.WriteLine(
"Service {0} has changed, State is {1}",
((ManagementBaseObject)e["TargetInstance"])["Name"],
((ManagementBaseObject)e["TargetInstance"])["State"]);
//Cancel the subscription
watcher.Stop();
return 0;
}
}
__________________
Skin
|