hitman
Analytics Guru
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:
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.
Then inside the loop, put this code to display all Computer Ports in the ListView.
Full source code:
Press F5 to run the program.
Output:
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
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:

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.
- 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.
- ListBox1.
Items
.
Add
(
portName)
;
Full source code:
- using
System.Diagnostics
;
- using
System
;
- using
System.Windows.Forms
;
- using
System.Collections
;
- using
System.Drawing
;
- using
System.Data
;
- using
System.Collections.Generic
;
- namespace
WindowsApplication3
- {
- public
partial
class
Form1
- {
- public
Form1(
)
- {
- InitializeComponent(
)
;
- }
- public
void
Button1_Click(
System
.
Object
sender, System
.
EventArgs
e)
- {
- foreach
(
string
portName in
(
new
Microsoft.
VisualBasic
.
Devices
.
Computer
(
)
)
.
Ports
.
SerialPortNames
)
- {
- ListBox1.
Items
.
Add
(
portName)
- }
- }
- }
- }
Press F5 to run the program.
Output:

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.