Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
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 22-Jul-10 22:46pm
Edited 23-Jul-10 3:36am
v4
Comments
Alison P at 23-Jul-10 4:49am
   
You have neglected to ask a question. Please explain what it is that you need help with. :)
Sandeep Mewara at 23-Jul-10 4:52am
   
Refresh object when? where? any details? What you tried?
Ramya 2010 at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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();
    }
}
 
The constants involved are: WM_DEVICECHANGE = 0x0219, DBT_DEVICEARRIVAL = 0x8000, DBT_DEVICEREMOVECOMPLETE = 0x8004
 
Smile | :)
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 478
1 Sergey Alexandrovich Kryukov 305
2 PhilLenoir 115
3 RajeeshMenoth 110
4 Uma Shankar Patel 80
0 Sergey Alexandrovich Kryukov 6,557
1 OriginalGriff 5,966
2 CPallini 2,473
3 Richard MacCutchan 1,687
4 Abhinav S 1,540


Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 23 Jul 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100