列出可用的COM端口

use*_*935 12 c# forms windows

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

我的问题是:

有没有一种简单的方法让程序在托盘中运行,只有在新的COM端口可用时才弹出,是否可以添加COM端口的名称,您可以在设备管理器ec"USB串口"中看到该名称?

我经常添加/删除一个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)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

}

Md.*_*tan 11

 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();
    }
Run Code Online (Sandbox Code Playgroud)


Mat*_*ira 7

看看这个问题.它使用WMI查找可用的COM端口.您可以跟踪存在的COM端口,并仅通知新端口.


Ben*_*igt 5

要找出何时热插拔设备,您需要处理WM_DEVICECHANGE.致电RegisterDeviceNotification以启用这些通知的传递.