Hi everyone, I'm trying to catch Win32 events to detect plugged in drives such as usb and external hard disks. I've imported the System.Management.dll from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ and then wrote the script like this
var watcher = new ManagementEventWatcher();
var query = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent");
watcher.EventArrived += (s, e) =>
{
//log event properties
};
watcher.Query = query;
watcher.Start();
However, this raises an error when the thread starts:
NullReferenceException: Object reference not set to an instance of an object
System.Management.ThreadDispatch.Start () (at :0)
System.Management.ManagementScope.Initialize () (at :0)
System.Management.ManagementObjectSearcher.Initialize () (at :0)
System.Management.ManagementObjectSearcher.Get () (at :0)
Does someone have any experience with this?
The same code works when inside a simple console application.
↧