My.Computer.Ports.SerialPortNames 属性

更新:2007 年 11 月

获取计算机上串行端口的名称的集合。

' 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)

返回值

计算机上串行端口的名称的集合。

备注

My.Computer.Ports.SerialPortNames 属性获取计算机上串行端口的名称的集合。

说明:

当在 Windows 98 上运行时,My.Computer.Ports.SerialPortNames 返回的端口名称可能不正确。在使用端口名称打开端口时,为防止应用程序错误,请使用异常处理,如 Try...Catch...Finally 语句或 Using 语句。

任务

下表列出了涉及 My.Computer.Ports.SerialPortNames 属性的任务的示例。

要执行的操作

请参见

显示可用的串行端口

如何:在 Visual Basic 中显示可用的串行端口

示例

本示例循环访问 My.Computer.Ports.SerialPortNames 属性返回的所有字符串。这些字符串是计算机上可用的串行端口的名称。

通常,用户从可用端口的列表中选择应用程序应使用的串行端口。在本示例中,串行端口名称存储在 ListBox 控件中。有关更多信息,请参见 ListBox 控件(Windows 窗体)

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

此示例需要:

  • System 命名空间的引用。

  • 窗体具有一个名为 ListBox1 的 ListBox 控件。

有关更多信息,请参见如何:在 Visual Basic 中显示可用的串行端口

要求

命名空间:Microsoft.VisualBasic.Devices

类:Ports

**程序集:**Visual Basic 运行库(在 Microsoft.VisualBasic.dll 中)

按项目类型列出的可用性

项目类型

可用

Windows 应用程序

类库

控制台应用程序

Windows 控件库

Web 控件库

Windows 服务

网站

权限

不需要任何权限。

请参见

任务

如何:在 Visual Basic 中显示可用的串行端口

参考

My.Computer.Ports 对象

My.Computer.Ports.OpenSerialPort 方法

ReadOnlyCollection<T>

Ports.SerialPortNames

Try...Catch...Finally 语句 (Visual Basic)

Using 语句 (Visual Basic)