2014-01-14 34 views
2

如何列出所有可用com端口到我的電腦?如何通過asp.net 4獲得C#中所有可用Com端口的列表

我已經看到了幾個與.net 1.1有關的例子,所以我在遊蕩是不是一個更現代的方式去做這件事?

我有我的SerialPort稱爲serialPort

我從MSDN網站驗證碼:

// 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); 
} 

它給我以下錯誤: 名稱「的SerialPort」難道不是在當前背景下

存在

我試過把它改成小s,然後我得到這個錯誤:

Member 'System.IO.Ports.SerialPort.GetPortNames()' cannot be accessed with an instance  reference; qualify it with a type name instead  
+0

嗯,「字符串[] =端口System.IO.Ports.SerialPort。 GetPortNames();」工作正常,並找出我的「COM1」端口 –

+0

請檢查連接串行設備到您的Web服務器的智慧。這往往需要很長的電纜。 –

回答

3

The name "SerialPort" Does not exist in the current context錯誤表示您沒有在項目/文件中設置必需的引用和/或命名空間導入。

'System.IO.Ports.SerialPort.GetPortNames()' cannot be accessed with an instance reference; qualify it with a type name instead表示您正試圖在對象實例上調用靜態方法(顯然,這是不可能的)。

您需要在您的方法調用完全限定命名空間:

string[] ports = System.IO.Ports.SerialPort.GetPortNames(); 

或添加using指令:

using System.IO.Ports; 
..... 
string[] ports = SerialPort.GetPortNames(); 
相關問題