4
我试图监控驱动器本地PC。我对两个事件感兴趣:连接驱动器(USB驱动器,CD-ROM,网络驱动器等)并断开连接。我使用ManagementOperationObserver写了一个概念的快速证明,它部分工作。现在(下面的代码),我正在得到各种事件。当驱动器连接并断开连接时,我只想获取事件。有没有办法在Wql查询中指定这个?使用WMI监控驱动器
谢谢!
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);
}
}
}
快速评论:您不需要使用ParameterizedThreadStart,因为您没有使用该参数。只需使用ThreadStart。 – 2010-10-25 21:48:20