How to find available COM ports in my PC? I am using framework v1.1. Is it possible to find all COM ports? If possible, help me solve the problem.
9 Answers
Framework v1.1 AFAIK doesn't allow you to do this.
In 2.0 there is a static function
SerialPort.GetPortNames()
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.getportnames.aspx

- 31,770
- 10
- 98
- 102
-
Probably you didn't install the driver properly. Does it appear in the hyperterminal? – Vanuan Sep 10 '12 at 11:06
-
Drivers are installed and other software can connect to them including hyperterminal. These are FTDI USB<->serial based devices. I ended up using the QueryDosDevices from this answer: http://stackoverflow.com/questions/1388871/how-do-i-get-a-list-of-available-serial-ports-in-win32 – Jon Cage Sep 11 '12 at 08:55
As others suggested, you can use WMI. You can find a sample in CodeProject
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)
{
Console.WriteLine("An error occurred while querying for WMI data: " + e.Message);
}
-
5This code produces error on the line "foreach" on .net Framework 4.0. – Nick Binnet Jun 03 '13 at 16:42
-
-
For Python you should be using pyserial - it has a built-in to query com ports (see http://pyserial.readthedocs.io/en/latest/tools.html - list ports) – drojf Nov 30 '17 at 03:27
The available serial ports can also be found at the values at the HKEY_LOCAL_MACHINE\hardware\devicemap\serialcomm
key in the registry.

- 33,059
- 4
- 45
- 36
How about asking a straight question from operating system:
using System;
using System.Collections.Generic;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
public class MyClass
{
private const uint GENERIC_ALL = 0x10000000;
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const uint GENERIC_EXECUTE = 0x20000000;
private const int OPEN_EXISTING = 3;
public const int INVALID_HANDLE_VALUE = -1;
public static void Main()
{
for (int i = 1; i <= 32; i++)
Console.WriteLine ("Port {0}: {1}", i, PortExists (i));
}
private static bool PortExists (int number) {
SafeFileHandle h = CreateFile (@"\\.\COM" + number.ToString (), GENERIC_READ + GENERIC_WRITE,
0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
bool portExists = !h.IsInvalid;
if (portExists)
h.Close ();
return portExists;
}
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern SafeFileHandle CreateFile (string lpFileName, System.UInt32 dwDesiredAccess,
System.UInt32 dwShareMode, IntPtr pSecurityAttributes, System.UInt32 dwCreationDisposition,
System.UInt32 dwFlagsAndAttributes, IntPtr hTemplateFile);
}

- 19,486
- 24
- 91
- 127
-
3OK...there is a catch! This will tell you if a port is available for the application. If a port is captured by another app, PortExists function will return false. If you want to get the list of port available in hardware (even if captured by other apps) then PInvoke GetDefaultCommConfig (http://msdn.microsoft.com/en-us/library/aa363262(VS.85).aspx). – Hemant Jul 04 '09 at 11:45
WMI contains a lot of hardware information. Query for instances of Win32_SerialPort.
(OTOH I can't recall how much WMI query support was in .NET 1.1.)

- 106,783
- 21
- 203
- 265
There is no support for SerialPort communication in .net v1.1. The most common solution for this was to use the MSCOMMCTL active X control from a VB6.0 installation (import into your .net project as a COM component from the add reference dialog box).
In later versions the Serial Port support is available through the System.IO.Ports name space. Also please note there is no API which will get you the list of free ports.
You can get a list of all the port names and then try opening a connection. An exception occurs if the port is already in use.

- 5,993
- 4
- 30
- 39
Use QueryDosDevice
API function. This is a VB6 snippet:
ReDim vRet(0 To 255)
sBuffer = String(100000, 1)
Call QueryDosDevice(0, sBuffer, Len(sBuffer))
sBuffer = Chr$(0) & sBuffer
For lIdx = 1 To 255
If InStr(1, sBuffer, Chr$(0) & "COM" & lIdx & Chr$(0), vbTextCompare) > 0 Then
vRet(lCount) = "COM" & lIdx
lCount = lCount + 1
End If
Next

- 11,771
- 1
- 33
- 41
Since you are using .net 1.1 one option is to use the AxMSCommLib control.
Here is a web page that assisted me in starting to use AxMSCommLib control. There is even a FindDevicePort() method listed that can be easily modified.
I have since switched to System.IO.Ports which appears to be much more robust.
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=320
Thanks
Joe

- 2,434
- 3
- 25
- 30
Maybe you will find this useful?
I am showing you a simple way to check all the COM ports in you PC. To get started follow these steps:
- Create a WinForms application in Visual Studio.
- Darg and drop a comboBox in your form and name it comboBoxCOMPORT
Copy the following code and paste after the public Form1() method (autogenerated).
private void Form1_Load(object sender, EventArgs e) { string[] ports = SerialPort.GetPortNames(); comboBoxCOMPORT.Items.AddRange(ports); }
- Run the app and click on drop down arrow on the comboBox to reveal all the available COM PORTS.
The above method works for Edgeport USB-to-serial converters as well as virtual ports. I implemented this in my project and works smoothly. Let me know if I can provide any further assistance.

- 261
- 1
- 9