C# WMI 中的软盘噪音 - Win32_LogicalDisk 类
我正在尝试使用 WMI 跟踪 Windows 上的 USB 设备插入和 CD/DVD 插入。但是当我使用 Win32_LogicalDisk 类来跟踪这些事件时,软盘开始发出噪音。
我的查询如下所示。第一个用于 USB,第二个用于 CD。
q = gcnew WqlEventQuery();
q->EventClassName = "__InstanceCreationEvent";
q->WithinInterval = TimeSpan(0, 0, 3);
q->Condition = "TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 2 and TargetInstance.DeviceID <> 'A:' and TargetInstance.DeviceID <> 'B:'";
w = gcnew ManagementEventWatcher(scope, q);
w->EventArrived += gcnew EventArrivedEventHandler(USBAdded);
w->Start();
q = gcnew WqlEventQuery();
q->EventClassName = "__InstanceModificationEvent";
q->WithinInterval = TimeSpan(0, 0, 3);
q->Condition = "TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5 and TargetInstance.DeviceID <> 'A:' and TargetInstance.DeviceID <> 'B:'";
w = gcnew ManagementEventWatcher(scope, q);
w->EventArrived += gcnew EventArrivedEventHandler(LogicalInserted);
w->Start();
事实上,它不会在所有版本上产生噪音。任何想法将不胜感激。
I am trying to track USB device insertions and CD/DVD insertions on Windows by using WMI. However when I use Win32_LogicalDisk class to track those events, floppy starts to make noise.
My queries are like below. First one is for USB and second one is for CD.
q = gcnew WqlEventQuery();
q->EventClassName = "__InstanceCreationEvent";
q->WithinInterval = TimeSpan(0, 0, 3);
q->Condition = "TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 2 and TargetInstance.DeviceID <> 'A:' and TargetInstance.DeviceID <> 'B:'";
w = gcnew ManagementEventWatcher(scope, q);
w->EventArrived += gcnew EventArrivedEventHandler(USBAdded);
w->Start();
q = gcnew WqlEventQuery();
q->EventClassName = "__InstanceModificationEvent";
q->WithinInterval = TimeSpan(0, 0, 3);
q->Condition = "TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5 and TargetInstance.DeviceID <> 'A:' and TargetInstance.DeviceID <> 'B:'";
w = gcnew ManagementEventWatcher(scope, q);
w->EventArrived += gcnew EventArrivedEventHandler(LogicalInserted);
w->Start();
Actually it does not make noise on all versions. Any idea will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基于此处的 Microsoft WMI 支持消息,我不确定 Win32_LogicalDisk 上的 WMI 查询是否能够在每个轮询间隔启动软盘的情况下运行。我正在尝试自己寻找解决此问题的替代方法;当我使用托管代码时,我正在考虑仅运行一个计时器并通过 DriveInfo.GetDrives 枚举可用驱动器。
更新:当我在 Windows 服务中执行此操作时,并且已经按照此 CodeProject 文章(但具有适当的异常处理和非托管内存清理),我只是添加了 DBT_DEVICEARRIVAL 和 DBT_DEVICEREMOVECOMPLETE 消息的处理程序。 (感谢 Chris Dickson 向我指出那篇文章此处。)我在处理程序中使用 DriveInfo.GetDrives 来确定已插入或删除了哪些设备,因为我发现这比通过 Win32 获取驱动器号更干净、更简单。不涉及定期轮询,没有混乱的 WMI,并且驱动器 A 现在保持良好且安静。
Based on a Microsoft WMI support message here, I'm not certain a WMI query on Win32_LogicalDisk is going to be able to run without firing up the floppies on each polling interval. I'm trying to find an alternate way of solving this issue myself; as I'm working in managed code, I'm considering just running a timer and enumerating the available drives via DriveInfo.GetDrives.
Update: As I was doing this in a Windows service and already had implemented a message handler along the lines described in this CodeProject article (but with proper exception handling and unmanaged memory cleanup), I simply added handlers for the DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE messages. (Due credit to Chris Dickson for pointing that article out to me here.) I used DriveInfo.GetDrives in the handler to determine which devices had been inserted or removed, as I found that cleaner and simpler than getting at the drive letter via Win32. There's no periodic polling involved, no messy WMI, and drive A now stays nice and quiet.
我从 WMI 创建了一种新方法。
之后我处理了生成的事件,如下所示:
I have created a new approach from WMI.
After that I have handled the generated event like below: