I just started to code a Programm which detects the USB Sticks and this is my current code:
public enum EventType
{
Inserted = 2,
Removed = 3
}
static void Main(string[] args){
ManagementEventWatcher watcher = new ManagementEventWatcher();
WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2 or EventType = 3");
watcher.EventArrived += (s, e) =>
{
string driveName = e.NewEvent.Properties["DriveName"].Value.ToString();
EventType eventType = (EventType)(Convert.ToInt16(e.NewEvent.Properties["EventType"].Value));
string eventName = Enum.GetName(typeof(EventType), eventType);
Console.WriteLine("{0}: {1} {2}", DateTime.Now, driveName, eventName);
};
watcher.Query = query;
watcher.Start();
Console.ReadKey();
}
But everytime i get this error: "System.TypeInitializationException: "The type initializer for 'System.Management.ManagementPath' threw an exception."
And i can not find a solution. And yes i imported System.Management; I would be happy if someone could help me.