C# 使用WMI监视驱动器

C# 使用WMI监视驱动器,c#,wmi,wmi-query,C#,Wmi,Wmi Query,我正在尝试监视本地PC上的驱动器。我对两个事件感兴趣:驱动器连接(USB驱动器、CD-ROM、网络驱动器等)和断开连接。我使用ManagementOperationObserver编写了一个快速的概念验证,它部分有效。现在(使用下面的代码),我得到了各种各样的事件。我只希望在连接和断开驱动器时获取事件。有没有办法在Wql查询中指定这一点 谢谢 private void button2_Click(object sender, EventArgs e) { t =

我正在尝试监视本地PC上的驱动器。我对两个事件感兴趣:驱动器连接(USB驱动器、CD-ROM、网络驱动器等)和断开连接。我使用ManagementOperationObserver编写了一个快速的概念验证,它部分有效。现在(使用下面的代码),我得到了各种各样的事件。我只希望在连接和断开驱动器时获取事件。有没有办法在Wql查询中指定这一点

谢谢

    private void button2_Click(object sender, EventArgs e)
    {
        t = new Thread(new ParameterizedThreadStart(o =>
        {
            WqlEventQuery q;
            ManagementOperationObserver observer = new ManagementOperationObserver();

            ManagementScope scope = new ManagementScope("root\\CIMV2");
            scope.Options.EnablePrivileges = true;

            q = new WqlEventQuery();
            q.EventClassName = "__InstanceOperationEvent";
            q.WithinInterval = new TimeSpan(0, 0, 3);
            q.Condition = @"TargetInstance ISA 'Win32_LogicalDisk' ";
            w = new ManagementEventWatcher(scope, q);

            w.EventArrived += new EventArrivedEventHandler(w_EventArrived);
            w.Start();
        }));

        t.Start();
    }

    void w_EventArrived(object sender, EventArrivedEventArgs e)
    {
        //Get the Event object and display its properties (all)
        foreach (PropertyData pd in e.NewEvent.Properties)
        {
            ManagementBaseObject mbo = null;
            if ((mbo = pd.Value as ManagementBaseObject) != null)
            {
                this.listBox1.BeginInvoke(new Action(() => listBox1.Items.Add("--------------Properties------------------")));
                foreach (PropertyData prop in mbo.Properties)
                    this.listBox1.BeginInvoke(new Action<PropertyData>(p => listBox1.Items.Add(p.Name + " - " + p.Value)), prop);
            }
        }
    }
private void按钮2\u单击(对象发送者,事件参数e)
{
t=新线程(新参数化线程启动(o=>
{
wqleventqueryq;
ManagementOperationObserver observer=新的ManagementOperationObserver();
ManagementScope=新的ManagementScope(“根\\CIMV2”);
scope.Options.EnablePrivileges=true;
q=新的WqlEventQuery();
q、 EventClassName=“\uuuu InstanceOperationEvent”;
q、 WithinInterval=新的时间跨度(0,0,3);
q、 条件=@“TargetInstance ISA'Win32_LogicalDisk'”;
w=新的ManagementEventWatcher(范围,q);
w、 EventArrived+=新的EventArrivedEventHandler(w_EventArrived);
w、 Start();
}));
t、 Start();
}
void w_EventArrived(对象发送方,EventArrivedEventArgs e)
{
//获取事件对象并显示其属性(全部)
foreach(e.NewEvent.Properties中的PropertyData pd)
{
ManagementBaseObject mbo=null;
if((mbo=pd.Value作为ManagementBaseObject)!=null)
{
this.listBox1.BeginInvoke(新操作(()=>listBox1.Items.Add(“------------Properties-------------------------------”);
foreach(mbo.Properties中的PropertyData属性)
this.listBox1.BeginInvoke(新操作(p=>listBox1.Items.Add(p.Name+“-”+p.Value)),prop);
}
}
}

你就快到了。要区分连接到机器的驱动器和卸下的驱动器,需要检查
e.NewEvent
是否分别是或的实例。大致如下:

ManagementBaseObject baseObject = (ManagementBaseObject) e.NewEvent;

if (baseObject.ClassPath.ClassName.Equals("__InstanceCreationEvent"))
    Console.WriteLine("A drive was connected");
else if (baseObject.ClassPath.ClassName.Equals("__InstanceDeletionEvent"))
    Console.WriteLine("A drive was removed");
此外,还可以通过
TargetInstance
属性获取Win32_LogicalDisk实例

ManagementBaseObject logicalDisk = 
               (ManagementBaseObject) e.NewEvent["TargetInstance"];

Console.WriteLine("Drive type is {0}", 
                  logicalDisk.Properties["DriveType"].Value);

快速评论:您不需要使用ParameterizedThreadStart,因为您没有使用该参数。只需使用ThreadStart。