我正在尝试使用以下 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 上工作?