如何刷新管理对象 [英] How to refresh management object
本文介绍了如何刷新管理对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码,
Here is my Code,
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("SELECT * FROM Win32_USBControllerDevice");
foreach (ManagementObject mo in searcher.Get())
{
string str1 = mo["CurrentRefreshRate"].ToString();
Console.WriteLine(str1);
string dependent = mo["Dependent"].ToString();
string deviceId = dependent.Split(''='')[1];
deviceId = deviceId.Replace(''\"'', ''\'');
ManagementObjectSearcher searcher2 =
new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity Where DeviceID = " + deviceId);
foreach (ManagementObject mo2 in searcher2.Get())
{
HardwareDetails Detail = new HardwareDetails();
Detail.Description = mo2["Description"].ToString();
Detail.DeviceId = mo2["DeviceId"].ToString();
string[] str = Detail.DeviceId.Split(''\\'');
string Id = str[1];
if (Id.Contains(''&''))
{
string[] separate = Id.Split(''&'');
Detail.Vid = separate[0].Contains(''_'') ? separate[0].Split(''_'')[1] : separate[0].Split(''D'')[1];
Detail.Pid = separate[1].Contains(''_'') ? separate[1].Split(''_'')[1] : separate[1].Split(''D'')[1];
//Detail.Pid = pid1[1];
}
else
{
Detail.Vid = "";
Detail.Pid = "";
}
if (list.Count > 0)
{
foreach (HardwareDetails h in list)
{
if (!(h.Description == Detail.Description))
{
list.Add(Detail);
break;
}
}
}
else
list.Add(Detail);
}
}
// remove duplicates, sort alphabetically and convert to array
HardwareDetails[] usbDevices = list.ToArray();
return usbDevices;
我已经编写了代码来显示连接的USB设备的描述(名称).删除设备后,我需要刷新ManagementObject并仅显示仍连接的设备的描述.
我该怎么做?
I had written code to display the description(Name) of connected USB devices. Once I remove a device I need to refresh the ManagementObject and have to display only the description of device still connected.
How do I do this?
推荐答案
假设使用WinForm应用程序,您可以覆盖表单的标准WndProc,并捕获有关要添加/删除和删除的设备的广播消息.根据这些信号采取行动.这是骨架:
Assuming a WinForm app, you could override your Form''s standard WndProc and catch the broadcast messages about devices being added/removed and act on those signals. Here is a skeleton:
protected override void WndProc(ref Message m) {
if (m.Msg==WM_DEVICECHANGE) {
int wParam=(int)m.WParam;
if (wParam==DBT_DEVICEARRIVAL) DeviceAdded();
if (wParam==DBT_DEVICEREMOVECOMPLETE) DeviceRemoved();
}
}
涉及的常量为:WM_DEVICECHANGE = 0x0219,DBT_DEVICEARRIVAL = 0x8000,DBT_DEVICEREMOVECOMPLETE = 0x8004
:)
The constants involved are: WM_DEVICECHANGE = 0x0219, DBT_DEVICEARRIVAL = 0x8000, DBT_DEVICEREMOVECOMPLETE = 0x8004
:)
这篇关于如何刷新管理对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文