a function to list all com ports in csharp

You can use the System.IO.Ports.SerialPort.GetPortNames method to retrieve a string array of all available COM ports on the current system. Here's an example code:

main.cs
using System.IO.Ports;

string[] ports = SerialPort.GetPortNames();

Console.WriteLine("Available COM Ports:");

foreach (string port in ports)
{
    Console.WriteLine(port);
}
177 chars
11 lines

This code will output a list of all available COM ports in the console. Note that you need to include the System.IO.Ports namespace for this to work.

gistlibby LogSnag