C# 如何使用asp.net 4获取C中所有可用Com端口的列表

C# 如何使用asp.net 4获取C中所有可用Com端口的列表,c#,serial-port,C#,Serial Port,如何列出计算机的所有可用com端口 我已经看到了一些使用.NET1.1的例子,所以我在想有没有一种更现代的方法来实现这一点 我有一个叫serialPort的serialPort 我从msdn站点获得了以下代码: // Get a list of serial port names. string[] ports = SerialPort.GetPortNames(); Console.WriteLine("The following serial ports were found:");

如何列出计算机的所有可用com端口

我已经看到了一些使用.NET1.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    
当前上下文中不存在名称SerialPort错误意味着您没有在项目/文件上设置所需的引用和/或命名空间导入

无法使用实例引用访问“System.IO.Ports.SerialPort.GetPortNames”;改为使用类型名限定它意味着您试图在对象实例上调用静态方法,这显然是不可能的

您需要在方法调用中完全限定名称空间:

string[] ports = System.IO.Ports.SerialPort.GetPortNames();
或添加一个using指令:

using System.IO.Ports;
.....
string[] ports = SerialPort.GetPortNames();
当前上下文中不存在名称SerialPort错误意味着您没有在项目/文件上设置所需的引用和/或命名空间导入

无法使用实例引用访问“System.IO.Ports.SerialPort.GetPortNames”;改为使用类型名限定它意味着您试图在对象实例上调用静态方法,这显然是不可能的

您需要在方法调用中完全限定名称空间:

string[] ports = System.IO.Ports.SerialPort.GetPortNames();
或添加一个using指令:

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

字符串[]port=System.IO.ports.SerialPort.GetPortNames;工作正常,了解我的COM1端口。查看将串行设备连接到web服务器的智慧。这往往需要很长的电缆;工作正常,了解我的COM1端口。查看将串行设备连接到web服务器的智慧。这往往需要很长的电缆。