Share via


My.Computer.Ports.SerialPortNames Property

Gets a collection of the names of the serial ports on the computer.

' Usage
Dim value As System.Collections.Generic.ReadOnlyCollection`1(Of String) = My.Computer.Ports.SerialPortNames
' Declaration
Public ReadOnly Property SerialPortNames As System.Collections.Generic.ReadOnlyCollection`1(Of String)

Property Value

A collection of the names of the serial ports on the computer.

Remarks

The My.Computer.Ports.SerialPortNames property gets a collection of the names of the serial ports on the computer.

Note

The port names returned by My.Computer.Ports.SerialPortNames may be incorrect when run on Windows 98. To prevent application errors, use exception handling, such as the Try...Catch...Finally statement or the Using statement, when using the port names to open ports.

Tasks

The following table lists an example of a task involving the My.Computer.Ports.SerialPortNames property.

To

See

Show available serial ports

How to: Show Available Serial Ports in Visual Basic

Example

This example loops over all the strings that the My.Computer.Ports.SerialPortNames property returns. These strings are the names of the available serial ports on the computer.

Typically, a user selects which serial port the application should use from the list of available ports. In this example, the serial port names are stored in a ListBox control. For more information, see ListBox Control (Windows Forms).

Sub GetSerialPortNames()
    ' Show all available COM ports. 
    For Each sp As String In My.Computer.Ports.SerialPortNames
        ListBox1.Items.Add(sp)
    Next 
End Sub

This example requires:

  • A reference to the System namespace.

  • That your form have a ListBox control named ListBox1.

For more information, see How to: Show Available Serial Ports in Visual Basic.

Requirements

Namespace:Microsoft.VisualBasic.Devices

Class:Ports

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

Availability by Project Type

Project type

Available

Windows Application

Yes

Class Library

Yes

Console Application

Yes

Windows Control Library

Yes

Web Control Library

No

Windows Service

Yes

Web Site

No

Permissions

No permissions are required.

See Also

Tasks

How to: Show Available Serial Ports in Visual Basic

Reference

My.Computer.Ports Object

My.Computer.Ports.OpenSerialPort Method

ReadOnlyCollection<T>

Ports.SerialPortNames

Try...Catch...Finally Statement (Visual Basic)

Using Statement (Visual Basic)