title: Listing USB Devices with C# and WMI
body: <p>I'm trying to implement a function in my application that lists all the plugged in USB Mass Storage Devices in a computer.</p>
  
  <p>My code works well when launching the application but my problem is that I want the box in my form to refresh automatically when a USB device is removed or attached.</p>
  
  <p>Implementing DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE conditions should work but I get back a "DisconnectedContext was detected" exception.</p>
  
  <p>I learned that I need to use a delegate and set an asyncronous call for this to work correctly.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>          public void listUSB()
        {
          ManagementScope sc = new ManagementScope(wmiUsbList);
  
          ObjectQuery query = new ObjectQuery("select * from Win32_DiskDrive " + "where InterfaceType='USB'");
  
          ManagementObjectSearcher searcher = new ManagementObjectSearcher(sc, query);
          ManagementObjectCollection result = searcher.Get();
  
          foreach (ManagementObject obj in result)
              {
                   if (obj["DeviceID"] != null)
                          {
                              usbListTextBox.AppendText(obj["Model"].ToString());
                          }
              }
  
        }
  </code></pre>
  
  <p>I'd really like to know how to apply a delegate to my method.</p>
  
  <p>I also looked at <a href="http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/ea183afd-d070-4abd-8e00-a1784fdfedc5/" rel="nofollow">this</a> thread on MSDN which provides an excellent example, but as of that example I am not able to understand how to put the deviceList in a textbox.</p>
  
  <p>I'm still Learning so if someone could be so kind to point me to the right direction on one or both of my questions, that would be greatly appreciated.</p>
  
  <p>Thanks!</p>