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

时间:2011-04-13 21:19:48

标签: .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();

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

2 个答案:

答案 0 :(得分:3)

基于Microsoft WMI支持消息here,我不确定Win32_LogicalDisk上的WMI查询是否能够在不在每个轮询间隔启动软盘的情况下运行。我正在努力寻找另一种解决这个问题的方法;当我在托管代码中工作时,我正在考虑运行一个计时器并通过DriveInfo.GetDrives枚举可用的驱动器。

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

答案 1 :(得分:0)

我已经从WMI创建了一种新方法。

void MyDLPWMIDeviceListener::AddInsertUSBHandler()
{
        WqlEventQuery ^q;
        ManagementEventWatcher ^w;
        ManagementScope ^scope = gcnew ManagementScope("root\\CIMV2");
        scope->Options->EnablePrivileges = true;
        try
        {
            q = gcnew WqlEventQuery();
            q->EventClassName = "__InstanceCreationEvent";
            q->WithinInterval = TimeSpan(0, 0, 3);
        q->Condition = "TargetInstance ISA 'Win32_USBControllerDevice'";
            w = gcnew ManagementEventWatcher(scope, q);
            w->EventArrived += gcnew EventArrivedEventHandler(USBAdded);
            w->Start();
        }
        catch (Exception ^ex)
        {
            if (w != nullptr)
                w->Stop();
        }
}

之后我处理了生成的事件,如下所示:

void MyDLPWMIDeviceListener::USBAdded(Object ^sender, EventArrivedEventArgs ^e)
    {   
        try {

            PropertyData ^pd = e->NewEvent->Properties["TargetInstance"];
            if (pd != nullptr)
            {
                ManagementBaseObject ^mbo = dynamic_cast<ManagementBaseObject ^>(pd->Value);
                if(mbo != nullptr && mbo->Properties["Dependent"] != nullptr
                    && mbo->Properties["Dependent"]->Value != nullptr) {
                    String ^str = (String ^)mbo->Properties["Dependent"]->Value;
                    str = str->Replace("\"","");
                    String ^splitChar = "=";
                    array<String ^> ^strArr = str->Split(splitChar->ToCharArray());

                    WqlObjectQuery ^wqlQuery = gcnew WqlObjectQuery("Select * from Win32_PnPEntity where DeviceID = '"+strArr[1]+"'");
                    ManagementObjectSearcher ^searcher = gcnew ManagementObjectSearcher(wqlQuery);
                    for each (ManagementObject ^usbCont in searcher->Get()) {
                        String ^pnpDeviceID = (String ^)usbCont->Properties["PNPDeviceID"]->Value;
                        splitChar = "\\";
                        array<String ^> ^pnpDeviceIDArr = pnpDeviceID->Split(splitChar->ToCharArray());
                        if(pnpDeviceIDArr->Length == 3) {
                            if(pnpDeviceIDArr[0] == "USB") {
                                WqlObjectQuery ^wqlQueryDisk = gcnew WqlObjectQuery("Select * from Win32_DiskDrive where PNPDeviceID LIKE '%"+pnpDeviceIDArr[2]+"%'");
                                ManagementObjectSearcher ^searcherDisk = gcnew ManagementObjectSearcher(wqlQueryDisk);
                                ManagementObjectCollection ^collectionDisk = searcherDisk->Get();
                                if(collectionDisk->Count == 0)
                                    continue;
                                else if (collectionDisk->Count == 1) {
                                    for each (ManagementObject ^disk in collectionDisk) {

                                    }
                                }
                                else {
                                    return;
                                }
                            } else {
                                return;
                            }
                        } else {
                            return;
                        }
                    }                   
                }
            }       
        } catch (Exception ^ex) {
        } 
    }
相关问题