Question by
AzeExMachina · May 21, 2019 at 08:23 AM ·
windowsdllsystemusbmanagement
Error in using WMI to catch logical drives events
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 <a2edda3557ef4ca8b6e3156a8719c2a2>:0)
System.Management.ManagementScope.Initialize () (at <a2edda3557ef4ca8b6e3156a8719c2a2>:0)
System.Management.ManagementObjectSearcher.Initialize () (at <a2edda3557ef4ca8b6e3156a8719c2a2>:0)
System.Management.ManagementObjectSearcher.Get () (at <a2edda3557ef4ca8b6e3156a8719c2a2>:0)
Does someone have any experience with this? The same code works when inside a simple console application.
Comment
Your answer
Follow this Question
Related Questions
Sending output to USBm device (Scent Palette) 0 Answers
How to patch on Windows build game 0 Answers
transfer data between android to Window throught a USB cable 0 Answers
OSX - using System.Drawing 0 Answers