Try adding the System.IO.Ports NuGet package:

<PackageReference Include="System.IO.Ports" Version="7.0.0" />

Also note that the fully qualified name is System.IO.Ports.SerialPort, not System.IO.SerialPort

Answer from Marc Gravell on Stack Overflow
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.io.ports.serialport
SerialPort Class (System.IO.Ports) | Microsoft Learn
Public Shared Function SetPortName(defaultPortName As String) As String Dim portName As String Console.WriteLine("Available Ports:") For Each s As String In SerialPort.GetPortNames() Console.WriteLine(" {0}", s) Next Console.Write("Enter COM port value (Default: {0}): ", defaultPortName) portName = Console.ReadLine() If portName = "" OrElse Not (portName.ToLower()).StartsWith("com") Then portName = defaultPortName End If Return portName End Function ' Display BaudRate values and prompt user to enter a value.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.io.ports.serialport.getportnames
SerialPort.GetPortNames Method (System.IO.Ports) | Microsoft Learn
Gets an array of serial port names for the current computer. public: static cli::array <System::String ^> ^ GetPortNames();
Discussions

C# .Net 6.0 System.IO.Ports is depricated. How can I use SerialPort? - Stack Overflow
However, this code is not working on .NET 6.0. System.IO is exist but .Ports does not. So Ports.SerialPort... all removed. How can I get Serial Ports list with .NET 6.0? More on stackoverflow.com
🌐 stackoverflow.com
c# - Why System.IO.Ports.SerialPort.GetPortNames() produces a DirectoryNotFoundException? - Stack Overflow
Just one line: System.IO.Ports.SerialPort.GetPortNames(); It works well when debugging, but the published version will produce a DirectoryNotFoundException: System.IO.DirectoryNotFoundException: C... More on stackoverflow.com
🌐 stackoverflow.com
February 25, 2021
System.IO.Ports isn't finding every com port?
it's nice that you include all the screenshots, but where is the code that actually finds the ports? More on reddit.com
🌐 r/csharp
19
27
August 8, 2024
IL2CPP NotSupportedException SerialPort::GetPortNames - Unity Engine - Unity Discussions
I’m attempting to use the method SerialPort.GetPortNames from System.IO.Ports and getting the following message from the Windows 64-bit standalone player: NotSupportedException: System.IO.Ports.SerialPort::GetPortNames at System.IO.Ports.SerialPort.GetPortNames () [0x00000] in :0 at ... More on discussions.unity.com
🌐 discussions.unity.com
0
March 28, 2022
🌐
NuGet
nuget.org › packages › system.io.ports
NuGet Gallery | System.IO.Ports 10.0.8
using System.IO.Ports; // Provides list of available serial ports string[] portNames = SerialPort.GetPortNames(); // First available port string myPortName = portNames[0]; int baudRate = 9600; SerialPort sp = new SerialPort(myPortName, baudRate); ...
🌐
Sparx Engineering
sparxeng.com › home › blog › if you *must* use .net system.io.ports.serialport
If you *must* use .NET System.IO.Ports.SerialPort - Sparx Engineering
May 2, 2023 - Port selection: constructors, PortName property, Open method, IsOpen property, GetPortNames method · And the one member that no one uses because MSDN gives no example, but is absolutely essential to your sanity: ... The only serial port read approaches that work correctly are accessed via BaseStream. Its implementation, the System.IO.Ports.SerialStream class (which has internal visibility; you can only use it via Stream virtual methods) is also home to the few lines of code which I wouldn’t choose to rewrite.
Top answer
1 of 2
3

There is no single built-in Windows command that lists "All Ports + Busy Status" in one view. We effectively have two approaches: the Non-Invasive method (inspecting OS handles) or the Native method (trying to open the port to see if it fails).

Method 1: The Non-Invasive Way

If we want to check port status without physically opening/closing them (which can reset devices), one must inspect the Windows Object Manager handles. This requires looking at who is holding the port, rather than asking the port if it is open.

Option A: Sysinternals Handle (Immediate) This is the industry standard for inspecting port usage. It is not built-in, but can be installed via Winget/Scoop/Choco (winget install Microsoft.Sysinternals.Handle).

Run as Administrator:

handle -a "Device\Serial"

  • Empty Result: The port is free.
  • Result Found: It lists exactly which process (PID and Name) is hogging the port.

Option B: Native openfiles (Requires Reboot) If one cannot install tools, you can use the native openfiles command, but it requires enabling the system-wide "Maintain Objects List" flag, which costs a small amount of RAM and requires a reboot.

  1. Enable (run once): openfiles /local on
  2. Reboot.
  3. Check ports: openfiles /query /FO TABLE | findstr "Serial"

Method 2: The Native PowerShell Script (Invasive)

If you cannot use external tools or reboot, you must use the "Try/Catch" method. This script iterates through all known ports, attempts to open them, and infers their status based on whether the OS throws an "Access Denied" error.

Note: This is "invasive"—opening the port, even for a millisecond, may cause DTR/RTS lines to toggle, potentially resetting connected hardware (like Arduino/ESP32 boards).

# Get all physically/logically present serial ports
$ports = [System.IO.Ports.SerialPort]::GetPortNames()
$portStatuses = @()

foreach ($port in $ports) {
    $status = 'Available'
    
    try {
        # Attempt to open and immediately close the port
        $testPort = New-Object System.IO.Ports.SerialPort $port
        $testPort.Open()
        $testPort.Close()
    }
    catch {
        # If Open() fails, the port is likely in use by another app (or PS itself)
        $status = 'Busy'
    }

    $portStatuses += [PSCustomObject]@{
        PortName = $port
        Status   = $status
    }
}

# Display cleanly
$portStatuses | Format-Table -AutoSize

Why the original commands were confusing:

  • [System.IO.Ports.SerialPort]::getportnames() only queries the registry for existence, so it always lists the port even if it's busy.
  • mode only interacts with the driver configuration. If a port is actively locked by another process, mode often fails or hides it, leading to the discrepancy I saw where PowerShell "hides" the port from mode once it opens a handle.
2 of 2
1

Try this :

  1. Click on a start menu
  2. Go to "run"
  3. in the field type CMD
  4. execute this commands in this order: a) C:>powershell b) PS> Get-WMIObject Win32_SerialPort

If you are x64 so type win64.

It should work i guess.

🌐
C# Corner
c-sharpcorner.com › blogs › get-serial-port-names-in-c-sharp1
Get Serial Port Names in c#
February 16, 2012 - private void btnGetPortNames_C... } } } } Using the System.IO.Ports.SerialPort.GetPortNames() it will retrieve the all available serial port names....
🌐
GitHub
github.com › nanoframework › System.IO.Ports
GitHub - nanoframework/System.IO.Ports: 📦 System.IO.Ports library for .NET nanoFramework.
The GetPortNames method will give you a list of available ports: var ports = SerialPort.GetPortNames(); You can also directly specify the baud rate and other elements in the constructor: var port = new SerialPort("COM2", 115200); Each property ...
Starred by 11 users
Forked by 7 users
Languages   C#
Find elsewhere
🌐
Post.Byes
post.bytes.com › home › forum › topic › visual basic .net
System.IO.Ports.SerialPort.GetPortNames return wrog names above CO - Post.Byes
February 27, 2006 - DataSource = PortOut_List In the OK button: SerialPortIn.Po rtName = CMB_PortNumIn.T ext SerialPortOut.P ortName = CMB_PortNumOut. Text MY PROBLEM: with all COM numbers under 10, no problem, but with COM number above 9 (11 and 12 in my case), getportnames return "COM11c" and "COM12c"! Is anybody can "duplicate" this error? Is it a bug? ... Re: System.IO.Ports .SerialPort.Get PortNames return wrog names above CO Hi Sam, [color=blue][color=green] >>[/color][/color] "COM11c" and "COM12c"!
🌐
Stack Overflow
stackoverflow.com › questions › 66370581 › why-system-io-ports-serialport-getportnames-produces-a-directorynotfoundexcept
c# - Why System.IO.Ports.SerialPort.GetPortNames() produces a DirectoryNotFoundException? - Stack Overflow
February 25, 2021 - Just one line: System.IO.Ports.SerialPort.GetPortNames(); It works well when debugging, but the published version will produce a DirectoryNotFoundException: System.IO.DirectoryNotFoundException: C...
🌐
Microsoft Learn
learn.microsoft.com › en-us › archive › msdn-technet-forums › c53b23a4-12c3-43ba-9127-b74450ab874a
How to get more info about port using System.IO.Ports.SerialPort | Microsoft Learn
I would also like to have the app detect when the adaptor gets plugged in so it can then enable the open port button. ... Try using the SerialPort.GetPortNames() method to find out which ports are available.
🌐
Reddit
reddit.com › r/csharp › system.io.ports isn't finding every com port?
r/csharp on Reddit: System.IO.Ports isn't finding every com port?
August 8, 2024 -

Hello.

It seems as though visual studio isn't able to find my CP210x device on COM2 while it can find everything else?

I'm at a loss as to why this is happening. is it because its UART?

my device manager:

my code: in a nutshell it finds available com ports and connects to it to send messages.

what my program finds: COM2 is not shown

com read code: on form start, copy coms to drop down list

private void Form1_Load(object sender, EventArgs e)

{

string[] ports = SerialPort.GetPortNames();

comboBoxCOMPORT.Items.AddRange(ports);

}

🌐
Unity
discussions.unity.com › unity engine
IL2CPP NotSupportedException SerialPort::GetPortNames - Unity Engine - Unity Discussions
March 28, 2022 - I’m attempting to use the method SerialPort.GetPortNames from System.IO.Ports and getting the following message from the Windows 64-bit standalone player: NotSupportedException: System.IO.Ports.SerialPort::GetPortNames at System.IO.Ports.SerialPort.GetPortNames () [0x00000] in :0 at SerialTest.Start () [0x00000] in :0 The code is as follows: void Start() { // Get a list of serial port names. string[] por...
🌐
NuGet
nuget.org › packages › System.IO.Ports › 8.0.0
NuGet Gallery | System.IO.Ports 8.0.0
November 14, 2023 - using System.IO.Ports; // Provides list of available serial ports string[] portNames = SerialPort.GetPortNames(); // First available port string myPortName = portNames[0]; int baudRate = 9600; SerialPort sp = new SerialPort(myPortName, baudRate); ...
🌐
GitHub
github.com › mono › mono › blob › main › mcs › class › System › System.IO.Ports › SerialPort.cs
mono/mcs/class/System/System.IO.Ports/SerialPort.cs at main · mono/mono
public SerialPort (string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits) · { port_name = portName; baud_rate = baudRate; data_bits = dataBits; stop_bits = stopBits; this.parity = parity; } · static string GetDefaultPortName () { string[] ports = GetPortNames(); if (ports.Length > 0) { return ports[0]; } else { int p = (int)Environment.OSVersion.Platform; if (p == 4 || p == 128 || p == 6) return "ttyS0"; // Default for Unix ·
Author   mono
🌐
GitHub
github.com › dotnet › runtime › issues › 79196
System.PlatformNotSupportedException: System.IO.Ports is currently only supported on Windows. on Archlinux · Issue #79196 · dotnet/runtime
December 3, 2022 - //DEFAULT port sur /dev/ttyUSB0 string SERIALPORT = "/dev/ttyUSB0"; //Line below works List<string> ports = SerialPort.GetPortNames().ToList(); SERIALPORT = ports.Last(); //Crash on line below SerialPort _serialPort = new SerialPort(SERIALPORT, BAUDRATE, Parity.None, 8, StopBits.One); _serialPort.ReadTimeout = -1; // |Pas de timeout _serialPort.WriteTimeout = -1; // | _serialPort.Handshake = Handshake.None; _serialPort.DataReceived += _serialPort_DataReceived; Reactions are currently unavailable · No one assigned · area-System.IO.PortsbugpackagingRelated to packagingRelated to packaging ·
Author   dotnet