Jim Fell Jim Fell - 27 days ago 7
C# Question

How to scan for COM ports in C#?

Does C# provide an effective means of scanning the available COM ports? I would like to have a dropdown list in my application wherein the user can select one of the detected COM ports. Creating and populating the dropdown list is not a problem. I just need to know how to scan for the available COM ports using C#. I am using Microsoft Visual C# 2008 Express Edition. Thanks.

Answer

System.IO.Ports is the namespace you want.

SerialPort.GetPortNames will list all serial COM ports.

Unfortunately, parallel ports are not supported directly from C#, as they're very infrequently used except in legacy situations. That said, you can list them by querying the following registry key:

HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\PARALLEL PORTS

See the Microsoft.Win32 namespace for details.