SerialPort.GetPortNames Method

Note: This method is new in the .NET Framework version 2.0.

Gets an array of serial port names for the current computer.

Namespace: System.IO.Ports
Assembly: System (in system.dll)

public static string[] GetPortNames ()
public static String[] GetPortNames ()
public static function GetPortNames () : String[]

Return Value

An array of serial port names for the current computer.

Exception typeCondition


The serial port names could not be queried.

The order of port names returned from GetPortNames is not specified.

Use the GetPortNames method to query the current computer for a list of valid serial port names. For example, you can use this method to determine whether COM1 and COM2 are valid serial ports for the current computer.

In Windows 98 environments, the port names are obtained from the system registry (HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM). If the registry contains stale or otherwise incorrect data then the GetPortNames method will return incorrect data.

The following code example uses the GetPortNames method to display serial port names to the console.

using System;
using System.IO.Ports;

namespace SerialPortExample
    class SerialPortExample
        public static void Main()
            // Get a list of serial port names.
            string[] ports = SerialPort.GetPortNames();

            Console.WriteLine("The following serial ports were found:");

            // Display each port name to the console.
            foreach(string port in ports)



Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0

Community Additions