1

I'm using c# FW 4.0. I want to iterate on all the serial ports in my computer and get the full name of each one of them. For example, I would like to see "Prolific USB-to-Serial Comm Port(COM6)" and not just COM6.

enter image description here

This is my current code which gives me only the COM/1/6 etc...

           string[] ports = SerialPort.GetPortNames();


        foreach (string port1 in ports)
        {
            MessageBox.Show(port1);

        }
Itay.B
  • 3,991
  • 14
  • 61
  • 96

1 Answers1

3

You could make use of WMI, take a look at the WMI Reference

Answer by Juanma, Here.

try
{
    ManagementObjectSearcher searcher =
        new ManagementObjectSearcher("root\\WMI",
        "SELECT * FROM MSSerial_PortName");

    foreach (ManagementObject queryObj in searcher.Get())
    {
        Console.WriteLine("-----------------------------------");
        Console.WriteLine("MSSerial_PortName instance");
        Console.WriteLine("-----------------------------------");
        Console.WriteLine("InstanceName: {0}", queryObj["InstanceName"]);

        Console.WriteLine("-----------------------------------");
        Console.WriteLine("MSSerial_PortName instance");
        Console.WriteLine("-----------------------------------");
        Console.WriteLine("PortName: {0}", queryObj["PortName"]);

        //If the serial port's instance name contains USB 
        //it must be a USB to serial device
        if (queryObj["InstanceName"].ToString().Contains("USB"))
        {
            Console.WriteLine(queryObj["PortName"] + " 
            is a USB to SERIAL adapter/converter");
        }
    }
}
catch (ManagementException e)
{
    MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
}
Community
  • 1
  • 1
Dayan
  • 7,634
  • 11
  • 49
  • 76