SerialPort.GetPortNames 메서드

정의

현재 컴퓨터의 직렬 포트 이름이 포함된 배열을 가져옵니다.

public:
 static cli::array <System::String ^> ^ GetPortNames();
public static string[] GetPortNames ();
static member GetPortNames : unit -> string[]
Public Shared Function GetPortNames () As String()

반환

String[]

현재 컴퓨터의 직렬 포트 이름이 포함된 배열입니다.

예외

직렬 포트 이름을 쿼리하지 못한 경우

예제

다음 코드 예제에서는 콘솔에 GetPortNames 직렬 포트 이름을 표시 하는 방법을 사용 합니다.

#using <System.dll>

using namespace System;
using namespace System::IO::Ports;
using namespace System::ComponentModel;

void main()
{
    array<String^>^ serialPorts = nullptr;
    try
    {
        // Get a list of serial port names.
        serialPorts = SerialPort::GetPortNames();
    }
    catch (Win32Exception^ ex)
    {
        Console::WriteLine(ex->Message);
    }

    Console::WriteLine("The following serial ports were found:");

    // Display each port name to the console.
    for each(String^ port in serialPorts)
    {
        Console::WriteLine(port);
    }
}
using System;
using System.IO.Ports;

namespace SerialPortExample
{
    class SerialPortExample
    {
        public static void Main()
        {
            // Get a list of serial port names.
            string[] ports = SerialPort.GetPortNames();

            Console.WriteLine("The following serial ports were found:");

            // Display each port name to the console.
            foreach(string port in ports)
            {
                Console.WriteLine(port);
            }

            Console.ReadLine();
        }
    }
}
' Insert this code into a new VB Console application project, and set the
' startup object to Sub Main.

Imports System.IO.Ports

Module SerialPortExample

    Sub Main()
        ' Get a list of serial port names.
        Dim ports As String() = SerialPort.GetPortNames()

        Console.WriteLine("The following serial ports were found:")

        ' Display each port name to the console.
        Dim port As String
        For Each port In ports
            Console.WriteLine(port)
        Next port

        Console.ReadLine()

    End Sub
End Module

설명

반환 GetPortNames 된 포트 이름의 순서가 지정되지 않았습니다.

이 메서드를 GetPortNames 사용하여 현재 컴퓨터에서 유효한 직렬 포트 이름 목록을 쿼리합니다. 예를 들어 이 메서드를 사용하여 COM1 및 COM2가 현재 컴퓨터에 유효한 직렬 포트인지 여부를 확인할 수 있습니다.

포트 이름은 시스템 레지스트리(예: HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM))에서 가져옵니다. 레지스트리에 부실 또는 잘못된 데이터가 포함된 경우 메서드는 GetPortNames 잘못된 데이터를 반환합니다.

적용 대상