C# 列出可用的COM端口

C# 列出可用的COM端口,c#,windows,forms,C#,Windows,Forms,我有一个非常小的代码显示可用的COM端口 我的问题是: 有没有一种简单的方法可以让程序在托盘中运行,并且只有当新的COM端口可用时才会弹出,并且可以添加可以在设备管理器ec“USB串行端口”中看到的COM端口的名称 我经常添加/删除一个USB->RS232转换器,并发现它很麻烦,因为我必须进入设备管理器查看它分配给哪个COM端口。每次都不一样 也许已经有一个小应用程序可以做到这一点,但我还没有在谷歌上找到它 using System; using System.Windows.Forms; us

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

我的问题是:

有没有一种简单的方法可以让程序在托盘中运行,并且只有当新的COM端口可用时才会弹出,并且可以添加可以在设备管理器ec“USB串行端口”中看到的COM端口的名称

我经常添加/删除一个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)
    {
    }
}

}

看一看。它使用WMI查找可用的COM端口。您可以跟踪现有的COM端口,只通知新端口。

要了解设备何时热插拔,您需要处理。调用以启用这些通知的传递。

获取特定设备的COM编号的代码

List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
ManagementObjectSearcher searcher =
    new ManagementObjectSearcher("root\\CIMV2",
    "SELECT * FROM Win32_PnPEntity");
foreach (ManagementObject queryObj in searcher.Get())
{
    devices.Add(new USBDeviceInfo(
        (string)queryObj["DeviceID"],
        (string)queryObj["PNPDeviceID"],
        (string)queryObj["Name"]
    ));
}

foreach (USBDeviceInfo usbDevice in devices)
{
    if (usbDevice.Description != null)
    {
        if (usbDevice.Description.Contains("NAME OF Device You are Looking for")) //use your own device's name
        {
            int i = usbDevice.Description.IndexOf("COM");
            char[] arr = usbDevice.Description.ToCharArray();
            str = "COM" + arr[i + 3];
            if (arr[i + 4] != ')')
            {
                str += arr[i + 4];
            }
            break;
        }
    }
}

mySerialPort = new SerialPort(str);
List devices=new List();
ManagementObjectSearcher搜索器=
新的ManagementObjectSearcher(“root\\CIMV2”,
“从Win32_pPentity中选择*”;
foreach(searcher.Get()中的ManagementObject查询对象)
{
添加(新的USBDeviceInfo)(
(字符串)queryObj[“设备ID”],
(字符串)queryObj[“PNPDeviceID”],
(字符串)queryObj[“Name”]
));
}
foreach(设备中的USBDeviceInfo usbDevice)
{
if(usbDevice.Description!=null)
{
if(usbDevice.Description.Contains(“您正在查找的设备名称”)//使用您自己的设备名称
{
int i=usbDevice.Description.IndexOf(“COM”);
char[]arr=usbDevice.Description.ToCharArray();
str=“COM”+arr[i+3];
如果(arr[i+4]!='))
{
str+=arr[i+4];
}
打破
}
}
}
mySerialPort=新的串行端口(str);
 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();
    }