title: C# - Get COM Ports with their friendly name using WMI
body: <p>I'm developing a C# solution and I want to get the COM Ports, the description and the friendlyName (if they are bluetooth).</p>
  
  <p>After investigating a bit, I've found that I can get the COM Ports using <strong>WMI/CIMV2/Win32_PnPEntity</strong> by searching the <strong>Name</strong> and <strong>Description</strong> values.</p>
  
  <p>To find the friendly name I need to search on <strong>Win32_PnPSignedDriver</strong> and take the value of <strong>FriendlyName</strong></p>
  
  <p>Is there a way to match them to get a list like this?</p>
  
  <ul>
  <li>COM56 - Bluetooth device - MyBTHDeviceName1</li>
  <li>COM76 - Bluetooth device - MyBTHDeviceName2</li>
  <li>COM5 - Serial device -</li>
  </ul>
  
  <p>I attach the code that I have right now to get the first two fields.</p>
  
  <pre><code>// Method to retrieve the list of all COM ports.
          public static List&lt;PortInfo&gt; FindComPorts()
          {
              List&lt;PortInfo&gt; portList = new List&lt;PortInfo&gt;();
              ConnectionOptions options = PrepareOptions();
              ManagementScope scope = PrepareScope(Environment.MachineName, options, @"\root\CIMV2");
  
              // Prepare the query and searcher objects.
              ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0");
              ManagementObjectSearcher portSearcher = new ManagementObjectSearcher(scope, objectQuery);
  
              using (portSearcher)
              {
                  string caption = null;
                  // Invoke the searcher and search through each management object for a COM port.
                  foreach (ManagementObject currentObject in portSearcher.Get())
                  {
                      if (currentObject != null)
                      {
                          object currentObjectCaption = currentObject["Caption"];
                          if (currentObjectCaption != null)
                          {
                              caption = currentObjectCaption.ToString();
                              if (caption.Contains("(COM"))
                              {
                                  PortInfo portInfo = new PortInfo();
                                  portInfo.Name = caption.Substring(caption.LastIndexOf("(COM")).Replace("(", string.Empty).Replace(")", string.Empty);
                                  portInfo.Description = caption;
                                  portList.Add(portInfo);
                              }
                          }
                      }
                  }
              }
              return portList;
          }
  </code></pre>
  
  <p>Thanks by advance.</p>