WMI事件Win32_VolumeChangeEvent是否适用于Windows XP

时间:2022-08-02 01:14:06

I'm trying to use the following c# code to detect the attached/removed event of usb mass storage devices. I'm using the Win32_VolumeChangeEvent.

我正在尝试使用以下c#代码来检测usb大容量存储设备的附加/删除事件。我正在使用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 that this works fine on Vista but it doesn't work on XP at all (no events received). The Microsoft documentation says that this should work (http://msdn.microsoft.com/en-us/library/aa394516(VS.85).aspx). I googled for this quite a while and found other that have this problem too. But I also found a couple of articles which claim that this kind of query (mostly in vbscript) works with XP. But I cannot find some offical information from microsoft for this issue and I can't believe that Microsoft have overlooked this issue for three service packs.

问题是这在Vista上工作正常,但它根本不适用于XP(没有收到任何事件)。 Microsoft文档说这应该可以工作(http://msdn.microsoft.com/en-us/library/aa394516(VS.85).aspx)。我用谷歌搜索了一段时间,发现其他也有这个问题。但我也发现了一些文章声称这种查询(主要是在vbscript中)与XP一起使用。但我无法从微软那里找到关于这个问题的一些官方信息,我无法相信微软已经忽略了三个服务包的这个问题。

So my question is: has anybody used the Win32_VolumeChangeEvent with success on XP or can provide a link/explanation why it shouldn't work on XP?

所以我的问题是:有没有人在XP上成功使用Win32_VolumeChangeEvent或者可以提供一个链接/解释为什么它不适用于XP?

2 个解决方案

#1


As you can read in your own link, the Minimum Supported Client version for Win32_VolumeChangeEvent is Windows Vista. Anyway, as suggested here, you can execute a query within interval in the root\\CIMV2 scope. Here an example from a code of mine:

您可以在自己的链接中阅读,Win32_VolumeChangeEvent的最低支持客户端版本是Windows Vista。无论如何,如此处所建议的,您可以在根\\ CIMV2范围内的间隔内执行查询。这是我的代码中的一个例子:

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();
    }
}

#2


"Win32_VolumeChangeEvent .. is found only on Windows Server 2003" - source

“Win32_VolumeChangeEvent ..仅在Windows Server 2003上找到” - 来源

#1


As you can read in your own link, the Minimum Supported Client version for Win32_VolumeChangeEvent is Windows Vista. Anyway, as suggested here, you can execute a query within interval in the root\\CIMV2 scope. Here an example from a code of mine:

您可以在自己的链接中阅读,Win32_VolumeChangeEvent的最低支持客户端版本是Windows Vista。无论如何,如此处所建议的,您可以在根\\ CIMV2范围内的间隔内执行查询。这是我的代码中的一个例子:

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();
    }
}

#2


"Win32_VolumeChangeEvent .. is found only on Windows Server 2003" - source

“Win32_VolumeChangeEvent ..仅在Windows Server 2003上找到” - 来源