Discussion:
Using WMI querries to detect CD/Network/removeable-drives
(too old to reply)
Gregers
2008-02-10 18:11:01 UTC
Permalink
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
unknown
2009-09-22 12:33:30 UTC
Permalink
I have the same problem with floppy drives. I was wondering if you have solved it...



Posted as a reply to:

Using WMI querries to detect CD/Network/removeable-drives
10-Feb-08

H

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' o
(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 advanc
Gregers

EggHeadCafe - Software Developer Portal of Choice
WPF And The Model View View Model Pattern
http://www.eggheadcafe.com/tutorials/aspnet/ec832ac7-6e4c-4ea8-81ab-7374d3da3425/wpf-and-the-model-view-vi.aspx
Loading...