.net - C# WMI 中的软盘噪音 - Win32_LogicalDisk 类

标签 .net winapi wmi floppy

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

实际上它不会在所有版本上产生噪音。任何想法将不胜感激。

最佳答案

基于 Microsoft WMI 支持消息 here ,我不确定 Win32_LogicalDisk 上的 WMI 查询是否能够在每个轮询间隔不启动软盘的情况下运行。我正在尝试自己寻找解决此问题的替代方法;因为我正在使用托管代码,所以我正在考虑只运行一个计时器并通过 DriveInfo.GetDrives 枚举可用驱动器。

更新:因为我在 Windows 服务中执行此操作并且已经按照此 CodeProject article 中描述的方式实现了消息处理程序(但通过适当的异常处理和非托管内存清理),我只是为 DBT_DEVICEARRIVAL 和 DBT_DEVICEREMOVECOMPLETE 消息添加了处理程序。 (归功于 Chris Dickson 将那篇文章指出给我 here 。)我在处理程序中使用 DriveInfo.GetDrives 来确定插入或删除了哪些设备,因为我发现这比获取驱动器号更清晰、更简单通过 Win32。没有涉及定期轮询,没有困惑的 WMI,并且驱动器 A 现在保持良好和安静。

关于.net - C# WMI 中的软盘噪音 - Win32_LogicalDisk 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5655788/

相关文章:

powershell - 如何使用 WMI 获取计算机的当前 OU 并列出该 OU 中的所有其他计算机?

winapi - MONITOR 是否需要发布?如果是这样,如何?

c# - 如果 smtp 服务器需要授权,我如何使用 .NET 4 发送电子邮件?

C# 应用程序 : how to view the loaded assemblies

c# - 如何使用 C# Runspace 从本地计算机读取 Azure VM 中存在的远程 yaml 文件

c - 异步 ReadDirectoryChangesW - GetQueuedCompletionStatus 总是超时

c++ - 调用 EnterCriticalSection 时出错

.net - ManagementObject 和 ManagementBaseObject 之间有什么区别

wmi - 我如何像字符串一样在 WMIC 中转义逗号

.net - RabbitMQ、.NET 和多线程