title: WMIC.exe does not find plugged usb device
body: <p>In the following method I start a process for calling WMIC.exe in order to query the DeviceID of an attached usb device.
  The problem is that sometimes although the device is plugged WIMC returns no instance, as if the device is not plugged. Nevertheless at the same time the Device Manager shows the device under the "Ports(COM &amp; LPT)" which means that WMIC's information is not accurate. I mean that if the device had crashed or in any way there was something malfunctioning with the device and it would need some kind of reset it should also not be in the device manager list.</p>
  
  <p>The method:</p>
  
  <pre><code>private string DonglePortName()
      {
          ProcessStartInfo startInfo = new ProcessStartInfo();
          startInfo.FileName = "C:\\Windows\\System32\\wbem\\WMIC.exe";
          startInfo.Arguments = "PATH Win32_SerialPort  GET /VALUE";
          startInfo.RedirectStandardOutput = true;
          startInfo.UseShellExecute = false;
          startInfo.CreateNoWindow = true;
  
          Process processTemp = new Process();
          processTemp.StartInfo = startInfo;
          processTemp.EnableRaisingEvents = true;
  
          processTemp.Start();
          string output = processTemp.StandardOutput.ReadToEnd();
          int indexOfBGEntry = output.IndexOf("Bluegiga Bluetooth Low Energy");
          if(indexOfBGEntry &gt; -1)
          {
              string output_sub = output.Substring(indexOfBGEntry);
              string str = "DeviceID=";
              int i = output_sub.IndexOf(str);
              int start = i + str.Length;
  
              string substr = output_sub.Substring(start);
              int end = substr.IndexOf("\r");
  
              return output_sub.Substring(start, end);
          }
          return null;
      }
  </code></pre>
  
  <p>At that point executing on a cmd window:</p>
  
  <p><code>C:\\Windows\\System32\\wbem\\WMIC.exe PATH Win32_SerialPort  GET /VALUE</code> </p>
  
  <p>returns:</p>
  
  <pre><code>No Instance(s) Available.
  </code></pre>
  
  <p>when at the same time Device Manager shows the device (even after clicking scan for hardware changes). </p>
  
  <p>So the C# code above is not actually needed to demonstrate the problem but I am only having it here in case someone can propose another, more robust, way of getting the device port (in C#) or could point out the reason for the inaccuracy of WMIC.</p>
  
  <p>Another clue is that WMIC always returns <code>No Instance(s) Available</code> after an Exception (and crash) on another part of the code that reads/writes to the SerialPort. I then have to unplug and replug the usb device and then it is "viewed" again by WMIC. Nevertheless, the Device Manager shows the device when it is plugged at all times. </p>