title: WMI query shows a single USB serial COM port?
body: <p>The code below performs a WMI query to enumerate the working devices on my computer (the OS is windows 7). It retrieves one USB serial port when I have several devices connected to USB plugs. Therefore I have three questions on the topic: </p>
  
  <p>1) Does all USB plugs have a separate com port or is there one com port for all the USB? </p>
  
  <p>2) If I send an array of string to the USB serial port, how will it reach the specific device? </p>
  
  <p>3) Can I extract the USB port from this query to keep it as a string?</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Management;
  using System.Windows.Forms;
  
  namespace MyNamespace
  {
      class Program
      {
          static void Main(string[] args)
          {
              MyClass x = new MyClass();
              var com = x.GetCOMs();
              foreach (string port in com) 
              {
                  Console.WriteLine(port);
              }
              Console.ReadLine();
          }
  
      }
  
      class MyClass
      {
          public List&lt;string&gt; GetCOMs()
          {
              List&lt;string&gt; coms = new List&lt;string&gt;();
              try
              {
                  ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2",
                  "SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0");
  
                  foreach (ManagementObject obj in searcher.Get())
                  {
                      object captionObj = obj["Caption"];
                      if (captionObj != null)
                      {
                          string caption = captionObj.ToString();
                          if (caption.Contains("(COM"))
                          {
                              coms.Add(caption);
                          }
                      }
                  }
  
                  m_ParseCOMs(ref coms);
              }
              catch (ManagementException ex)
              {
                  MessageBox.Show("An error occurred while querying for WMI data: " + ex.Message);
                  return coms;
              }
  
              return coms;
          }
  
          private void m_ParseCOMs(ref List&lt;string&gt; comPorts)
          {
              string[] temp;
              List&lt;string&gt; temp2 = new List&lt;string&gt;();
              int index = 0;
              foreach (string s in comPorts)
              {
                  string temp3 = "";
                  temp = s.Split(' ');
                  temp3 += temp[temp.Length - 1] + " - ";
                  for (int i = 0; i &lt; temp.Length - 1; i++)
                  {
                      temp3 += temp[i] + " ";
                  }
                  temp2.Insert(index, temp3);
                  index++;
              }
              comPorts = temp2;
          }
      }
  }
  </code></pre>
  
  <p>Many thanks in advance!</p>