list all com ports in csharp

To list all available COM ports in C#, you can use the SerialPort static method GetPortNames(). Here's an example:

main.cs
using System.IO.Ports;

// ...

string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
    Console.WriteLine(port);
}
140 chars
10 lines

This code will output the name of each available COM port to the console. Note that you'll need to add a reference to the System.IO.Ports namespace at the top of your code file to use the SerialPort class.

gistlibby LogSnag