Gregers
2008-02-10 18:11:01 UTC
Hi
I want to detect whenever a CD is inserted/ejected, a network drive is
connected/disconnected and whenever a removeable storage device (USB stick,
SD card, USB harddisk, etc.) is attached/removed.
To do this i have been using WMI events:
WqlEventQuery q = new WqlEventQuery();
q.EventClassName = "__InstanceOperationEvent";
q.WithinInterval = new TimeSpan(0, 0, 5);
q.Condition = @"TargetInstance ISA 'Win32_DiskDrive' or
(TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5)";
ManagementEventWatcher w = new ManagementEventWatcher(q);
w.EventArrived += new EventArrivedEventHandler(this.SomethingEventHandler);
w.Start();
Using TargetInstance ISA 'Win32_DiskDrive' in the query takes care of
detecting both removeable devices and network drives, however it has the
sideeffect that it makes any floppy drives become active any time you querry
(in this instance every 5seconds). This has been an issue with some of our
customers, since they find the constant "rrrrrr..... rrrrrr....
RRrRRrrrRR...."-noice quiete nerving .
We have removed a similar problem from the CD/DVD drives (where it would be
constantly spinning) by using the second part of the querry, instead of
querrying directly for TargetInstance ISA 'Win32_CDROMDrive'.
Any input on how to write a query which only detects the specific drive
types which i am interested in is very welcome!
Thanks in advance
Gregers
I want to detect whenever a CD is inserted/ejected, a network drive is
connected/disconnected and whenever a removeable storage device (USB stick,
SD card, USB harddisk, etc.) is attached/removed.
To do this i have been using WMI events:
WqlEventQuery q = new WqlEventQuery();
q.EventClassName = "__InstanceOperationEvent";
q.WithinInterval = new TimeSpan(0, 0, 5);
q.Condition = @"TargetInstance ISA 'Win32_DiskDrive' or
(TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5)";
ManagementEventWatcher w = new ManagementEventWatcher(q);
w.EventArrived += new EventArrivedEventHandler(this.SomethingEventHandler);
w.Start();
Using TargetInstance ISA 'Win32_DiskDrive' in the query takes care of
detecting both removeable devices and network drives, however it has the
sideeffect that it makes any floppy drives become active any time you querry
(in this instance every 5seconds). This has been an issue with some of our
customers, since they find the constant "rrrrrr..... rrrrrr....
RRrRRrrrRR...."-noice quiete nerving .
We have removed a similar problem from the CD/DVD drives (where it would be
constantly spinning) by using the second part of the querry, instead of
querrying directly for TargetInstance ISA 'Win32_CDROMDrive'.
Any input on how to write a query which only detects the specific drive
types which i am interested in is very welcome!
Thanks in advance
Gregers