SerialPort.GetPortNames Yöntem

Tanım

Geçerli bilgisayar için bir dizi seri bağlantı noktası adı alır.

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

Döndürülenler

String[]

Geçerli bilgisayar için seri bağlantı noktası adları dizisi.

Özel durumlar

Seri bağlantı noktası adları sorgulanamadı.

Örnekler

Aşağıdaki kod örneği, konsolda GetPortNames seri bağlantı noktası adlarını görüntülemek için yöntemini kullanır.

#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

Açıklamalar

'den GetPortNames döndürülen bağlantı noktası adlarının sırası belirtilmemiş.

GetPortNames Geçerli seri bağlantı noktası adlarının listesi için geçerli bilgisayarı sorgulamak için yöntemini kullanın. Örneğin, COM1 ve COM2'nin geçerli bilgisayar için geçerli seri bağlantı noktaları olup olmadığını belirlemek için bu yöntemi kullanabilirsiniz.

Bağlantı noktası adları sistem kayıt defterinden (örneğin, HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM)) elde edilir. Kayıt defteri eski veya başka bir şekilde yanlış veriler GetPortNames içeriyorsa yöntem yanlış veriler döndürür.

Şunlara uygulanır