Home > DeveloperSection > Forums > how to get port names in c#
Brad Pitt
Brad Pitt

Total Post:61

Posted on    June-08-2013 7:48 AM

 C# C# 

 1 Reply(s)
 2025  View(s)
Rate this:
Hi Expert,

I need how to show all port name in our system using C#.

Please tell me how to do this.

Your help is great appreciated.

Vijay Shukla

Total Post:100

Posted on    June-08-2013 7:53 AM

Hello Brad Pitt!
Below I give you some line of code which is show the all ports in your system.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
    class Program
        static System.IO.Ports.SerialPort _serialPort;
        static void Main(string[] args)
            _serialPort = new System.IO.Ports.SerialPort();
            _serialPort.PortName = SetPortName(_serialPort.PortName);
        public static string SetPortName(string defaultPortName)
            string portName;

            Console.WriteLine("Available Ports:");
            foreach (string s in System.IO.Ports.SerialPort.GetPortNames())

            Console.Write("Default port({0}): ", defaultPortName);
            portName = Console.ReadLine();

            if (portName == "")
                portName = defaultPortName;
            return portName;

Modified On Jun-08-2013 08:26:00 AM

Don't want to miss updates? Please click the below button!

Follow MindStick