我试图启动一个事件时,有人在一个USB设备插入。现在,我很满足于简单的东西打印到控制台(在成品,将推出一个应用程序)。
这code是非常松散改编自:http://serverfault.com/questions/115496/use-wmi-to-detect-a-usb-drive-was-connected-regardless-of-whether-it-was-mounted
有两个问题: 1)我需要动态传递参数管理范围,因为这会电脑,我不使用或他的名字我不知道上安装。 2)我收到的时候我打电话w.Start(无效的命名空间除外);
任何想法,我做错了什么?
静态ManagementEventWatcher W = NULL;
静态无效的主要(字串[] args)
{
AddInstUSBHandler();
对于(;;);
}
公共静态无效USBRemoved(对象sneder,EventArgs的五)
{
Console.WriteLine(一个USB设备插入);
}
静态无效AddInstUSBHandler()
{
WqlEventQuery q;
管理范围范围=新的管理范围(HQ \\装置1);
scope.Options.EnablePrivileges = TRUE;
Q =新WqlEventQuery();
q.EventClassName + =_ InstanceCreationEvent;
q.WithinInterval =新的时间跨度(0,0,3);
q.Condition = @TargetInstance ISA'Win32_USBControllerdevice';
W =新ManagementEventWatcher(范围,Q);
w.EventArrived + =新EventArrivedEventHandler(USBRemoved);
w.Start();
}
解决方案
这行不正确 - >HQ \ DEV1
//管理范围范围=新的管理范围(HQ \\装置1);
请管理范围类
//建立到远程计算机的连接。
//替换的的FullComputerName节
//字符串\\\\ FullComputerName \\ \\根CIMV2与
//完整的计算机名或IP地址
//远程计算机。
管理范围范围=
新的管理范围(
\\\\ FullComputerName \\ \\根CIMV2);
I'm trying to launch an event when someone plugs in a USB device. For now, I'm content to simply print something to the console (in the finished product, it will launch an application).
This code is very loosely adapted from: http://serverfault.com/questions/115496/use-wmi-to-detect-a-usb-drive-was-connected-regardless-of-whether-it-was-mounted
There are two problems: 1) I need to pass the argument to Management scope dynamically because this will be installed on computers I don't use or whose name I don't know. 2) I'm getting an invalid namespace exception when I call w.Start();
Any ideas what I'm doing wrong?
static ManagementEventWatcher w=null;
static void Main(string[] args)
{
AddInstUSBHandler();
for(;;);
}
public static void USBRemoved(object sneder, EventArgs e)
{
Console.WriteLine("A USB device inserted");
}
static void AddInstUSBHandler()
{
WqlEventQuery q;
ManagementScope scope = new ManagementScope("HQ\\DEV1");
scope.Options.EnablePrivileges=true;
q=new WqlEventQuery();
q.EventClassName+="_InstanceCreationEvent";
q.WithinInterval=new TimeSpan(0,0,3);
q.Condition=@"TargetInstance ISA 'Win32_USBControllerdevice'";
w=new ManagementEventWatcher(scope,q);
w.EventArrived+=new EventArrivedEventHandler(USBRemoved);
w.Start();
}
解决方案
This line is incorrect -> "HQ\DEV1"
//ManagementScope scope = new ManagementScope("HQ\\DEV1");
Follow ManagementScope Class
// Make a connection to a remote computer.
// Replace the "FullComputerName" section of the
// string "\\\\FullComputerName\\root\\cimv2" with
// the full computer name or IP address of the
// remote computer.
ManagementScope scope =
new ManagementScope(
"\\\\FullComputerName\\root\\cimv2");
上一篇:Android的力量亲近JodaTime亲近、力量、Android、JodaTime
下一篇:什么ListActivity和ListView提供超越常规的活动和观?常规、ListActivity、ListView