Does the WMI Win32_VolumeChangeEvent run on Windows XP
I am trying to use the following C # code to detect the attached / removed event of USB drives. I am using Win32_VolumeChangeEvent.
// Initialize an event watcher and subscribe to events that match this query
var _watcher = new ManagementEventWatcher("select * from Win32_VolumeChangeEvent");
_watcher.EventArrived += OnDeviceChanged;
_watcher.Start();
void OnDeviceChanged(object sender, EventArrivedEventArgs args)
{
Console.WriteLine(args.NewEvent.GetText(TextFormat.Mof));
}
The problem is it works fine on Vista, but doesn't work at all on XP (no events received). Microsoft documentation says this should work ( http://msdn.microsoft.com/en-us/library/aa394516(VS.85).aspx ). I searched for it for quite some time and found others who have this problem. But I also found a couple of articles claiming that such a query (mostly in vbscript) works with XP. But I can't find any official information from Microsoft for this issue, and I can't believe Microsoft overlooked this issue for three service packs.
So my question is, has anyone successfully used Win32_VolumeChangeEvent in XP or can provide a link / explanation why it shouldn't work on XP?
a source to share
As you can read in yours, the minimum supported client version for Win32_VolumeChangeEvent
is Windows Vista. In any case, as suggested here , you can execute the query within an interval in the scope root\\CIMV2
. Here's an example from my code:
WqlEventQuery query;
ManagementScope scope;
ManagementEventWatcher watcher;
public void DoWork()
{
// Check if OS Version is earlier than Windows Vista
if (USBHandlerWorker.OSVersion() <= 6)
{
scope = new ManagementScope("root\\CIMV2");
scope.Options.EnablePrivileges = true;
query = new WqlEventQuery();
query.EventClassName = "__InstanceCreationEvent";
query.WithinInterval = new TimeSpan(0, 0, 1);
query.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'";
watcher = new ManagementEventWatcher(scope, query);
watcher.EventArrived += watcher_EventArrived;
watcher.Start();
}
else
{
watcher = new ManagementEventWatcher();
// The event types 2 and 3 are for plug and unplug events
query = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent " +
"WHERE EventType = 2 OR EventType = 3");
watcher.EventArrived += watcher_EventArrived;
watcher.Query = query;
watcher.Start();
}
}
a source to share