How to get a list of all available Com Ports in C# with asp.net 4

13,954

The name "SerialPort" Does not exist in the current context error means you don't have the required references and/or namespace imports set on your project/file.

'System.IO.Ports.SerialPort.GetPortNames()' cannot be accessed with an instance reference; qualify it with a type name instead means you are trying to call a static method on an object instance (which is, obviously, impossible).

You need to fully qualify namespaces in your method call:

string[] ports = System.IO.Ports.SerialPort.GetPortNames();

or add a using directive:

using System.IO.Ports;
.....
string[] ports = SerialPort.GetPortNames();
Share:
13,954
Zapnologica
Author by

Zapnologica

I am an enthusiastic developer indulging in the world of programming. in love with C# .net

Updated on June 04, 2022

Comments

  • Zapnologica
    Zapnologica almost 2 years

    How can I list all of the available com ports to my computer?

    I have seen a few examples wiht .net 1.1 so I was wandering is there is not a more modern way to go about doing this?

    I have my serialPort called serialPort

    I got this code from msdn site:

    // 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)
    {
        Console.WriteLine(port);
    }
    

    It give me the following error: The name "SerialPort" Does not exist in the current context

    I have tried changing it to a small s, and I then get this error:

    Member 'System.IO.Ports.SerialPort.GetPortNames()' cannot be accessed with an instance        reference; qualify it with a type name instead