SerialPort.GetPortNames Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan array nama port serial untuk komputer saat ini.
public:
static cli::array <System::String ^> ^ GetPortNames();
public static string[] GetPortNames ();
static member GetPortNames : unit -> string[]
Public Shared Function GetPortNames () As String()
Mengembalikan
Array nama port serial untuk komputer saat ini.
Pengecualian
Nama porta serial tidak dapat dikueri.
Contoh
Contoh kode berikut menggunakan GetPortNames metode untuk menampilkan nama port serial ke konsol.
#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
Keterangan
Urutan nama port yang dikembalikan GetPortNames tidak ditentukan.
GetPortNames Gunakan metode untuk mengkueri komputer saat ini untuk daftar nama port seri yang valid. Misalnya, Anda dapat menggunakan metode ini untuk menentukan apakah COM1 dan COM2 adalah port seri yang valid untuk komputer saat ini.
Nama port diperoleh dari registri sistem (misalnya, HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM). Jika registri berisi data yang kedaluarsa atau salah maka GetPortNames metode akan mengembalikan data yang salah.