• We just launched and are currently in beta. Join us as we build and grow the community.

Get and Display all COM Ports using C#

hitman

Analytics Guru
H Rep
0
0
0
Rep
0
H Vouches
0
0
0
Vouches
0
Posts
43
Likes
121
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Today in C#, i will going to teach you how to create a program that will get all Computer ports and display it in a ListView using C#. We all know that COM Ports are serial communications port on a PC.

Now, let's start this tutorial!


1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your program as Get all Computer Ports.

2. Next, add only one Button named Button1 and labeled it as "Get Computer Ports". Insert a ListBox named ListBox1 for displaying all the COM Ports. You must design your interface like this:

comdes.png


3. Put this code in your Button1_Click. This will trigger to get and display all the available COM Port in the ListBox.

We will have to create a For Each loop statement that has the variable portName as String that has all the value of the Serial Port names. The code below is the main syntax of how we get all the COM Ports.
  1. foreach

    (

    string

    portName in

    (

    new

    Microsoft.

    VisualBasic

    .

    Devices

    .

    Computer

    (

    )

    )

    .

    Ports

    .

    SerialPortNames

    )

Then inside the loop, put this code to display all Computer Ports in the ListView.
  1. ListBox1.

    Items

    .

    Add

    (

    portName)

    ;


Full source code:

  1. using

    System.Diagnostics

    ;
  2. using

    System

    ;
  3. using

    System.Windows.Forms

    ;
  4. using

    System.Collections

    ;
  5. using

    System.Drawing

    ;
  6. using

    System.Data

    ;
  7. using

    System.Collections.Generic

    ;



  8. namespace

    WindowsApplication3
  9. {
  10. public

    partial

    class

    Form1
  11. {
  12. public

    Form1(

    )
  13. {
  14. InitializeComponent(

    )

    ;

  15. }



  16. public

    void

    Button1_Click(

    System

    .

    Object

    sender, System

    .

    EventArgs

    e)
  17. {
  18. foreach

    (

    string

    portName in

    (

    new

    Microsoft.

    VisualBasic

    .

    Devices

    .

    Computer

    (

    )

    )

    .

    Ports

    .

    SerialPortNames

    )
  19. {
  20. ListBox1.

    Items

    .

    Add

    (

    portName)
  21. }

  22. }
  23. }

  24. }

Press F5 to run the program.


Output:

comout.png


Best Regards,

Engr. Lyndon Bermoy

IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,340

323,349

Top