Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;


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?
Posted
Updated 23-Jul-10 3:36am
v4
Comments
LittleYellowBird 23-Jul-10 4:49am    
You have neglected to ask a question. Please explain what it is that you need help with. :)
Sandeep Mewara 23-Jul-10 4:52am    
Refresh object when? where? any details? What you tried?
Ramya 2010 23-Jul-10 5:03am    
@Alison---i had written a code to display the description(Name) of connected USB devices.once i removed a device,then i need to refresh the ManagementObject and have to display the connected device description.
Ramya 2010 23-Jul-10 5:11am    
@Sandeep Mewara--- sorry i forgot to type my question.Actually my question is "i had written a code to display the description(Name) of connected USB devices.once i removed a device,then i need to refresh the ManagementObject and have to display the connected device description. how to do this?" - Ramya 2010 7 mins ago

1 solution

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:

C#
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();
    }
}


The constants involved are: WM_DEVICECHANGE = 0x0219, DBT_DEVICEARRIVAL = 0x8000, DBT_DEVICEREMOVECOMPLETE = 0x8004

:)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900