c# - 列出可用的 COM 端口

标签 c# windows forms

我有一个非常小的代码可以显示可用的 COM 端口。

我的问题是:

有没有一种简单的方法可以让程序在托盘中运行,并且仅在新的 COM 端口可用时弹出,并且是否可以添加您可以在设备管理器中看到的 COM 端口的名称 ec“USB serial港口”?

我经常添加/删除一个 USB->RS232 转换器,这让我很头疼,因为我必须进入设备管理器才能看到它分配给了哪个 COM 端口。每次都不一样

也许已经有一个小应用程序可以做到这一点,但我还没有在谷歌上找到它

using System;
using System.Windows.Forms;
using System.IO.Ports;

namespace Available_COMports

{
    public partial class Form1 : Form
    {
        public Form1()
    {
        InitializeComponent();

        //show list of valid com ports
        foreach (string s in SerialPort.GetPortNames())
        {
            listBox1.Items.Add(s);
        }  
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
    }
}

最佳答案

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

关于c# - 列出可用的 COM 端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5445847/

相关文章:

windows - 使用 PowerShell 检查内存类型(是否 ECC)

ios - 在 Windows 上开发 iOS 应用程序

forms - 浏览器是否将表单操作查询字符串参数与字段合并?

php - 如何根据下拉列表更改操作链接?

c# - Visual Studio 代码分析 - 创建新规则来计算方法中的行数

c# - WCF Restful POST 从服务器到客户端返回错误消息

c# - 生产者消费者使用公共(public) BlockingCollection 分离类

c# - 返回一个与声明的结构不同的结构,为什么它可以正确? C#

windows - 设置azure windows vm默认分辨率以运行selenium脚本

c# - 将 listboxfor 发送到 Controller