本文介绍了在C#中使用WMI检测iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码在将USB设备连接到计算机时对其进行检测:
I'm using the following code to detect a USB device when it's connected to the computer:
private ManagementEventWatcher m_watcher;
try
{
string queryString =
"SELECT * " +
" FROM __InstanceCreationEvent " +
" WITHIN " + pollingInterval +
" WHERE TargetInstance ISA 'Win32_USBHub'";
EventQuery processQuery = new EventQuery(queryString);
ManagementScope scope = new ManagementScope("root\\CIMV2");
scope.Options.EnablePrivileges = true;
m_watcher = new ManagementEventWatcher(scope, processQuery);
m_watcher.EventArrived += new System.Management.EventArrivedEventHandler(EventArrived);
m_watcher.Start();
}
private void EventArrived(object sender, EventArrivedEventArgs e)
{
var instance = ((PropertyData)(e.NewEvent.Properties["TargetInstance"]));
var obj = (ManagementBaseObject)instance.Value;
var args = new InsertEventArgs();
var description = (string)obj.Properties["Description"].Value;
var deviceId = (string) obj.Properties["DeviceID"].Value;
var status = (string) obj.Properties["Status"].Value;
当我连接USB钥匙和三星手机时,这可以正常工作.当我连接iPhone时,EventArrived
功能永远不会被使用.我使用了错误的事件吗?我在做什么错了?
This works correctly when i attach a usb-key and a Samsung mobile phone. When I attach an iPhone, the EventArrived
function is never hit. Am I using a wrong event? What am i doing wrong?
推荐答案
尝试以下代码:
var queryStr = "SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_PnPEntity'";
var watcher = new ManagementEventWatcher(queryStr);
watcher.EventArrived += DeviceChangeEventReceived;
watcher.Start();
事件处理程序:
private static void DeviceChangeEventReceived(object sender, EventArrivedEventArgs e)
{
var instance = ((PropertyData)(e.NewEvent.Properties["TargetInstance"]));
var obj = (ManagementBaseObject)instance.Value;
var description = (string)obj.Properties["Description"].Value;
var deviceId = (string)obj.Properties["DeviceID"].Value;
var status = (string)obj.Properties["Status"].Value;
}
这篇关于在C#中使用WMI检测iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!