WMI 事件 Win32_VolumeChangeEvent 在 Windows XP 上工作吗

发布于 2024-07-22 07:46:52 字数 936 浏览 8 评论 0原文

我正在尝试使用以下 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));
}

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

所以我的问题是:是否有人在 XP 上成功使用了 Win32_VolumeChangeEvent 或者可以提供链接/解释为什么它不能在 XP 上工作?

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.

  
// 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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谎言 2024-07-29 07:46:52

正如您可以在自己的链接中阅读的那样, Win32_VolumeChangeEvent 支持的最低客户端版本是 Windows Vista。 无论如何,按照此处的建议,您可以执行root\\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();
    }
}

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:

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();
    }
}
从﹋此江山别 2024-07-29 07:46:52

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文