C# WMI:获取所有串行(COM)端口(包括虚拟端口)的列表

C# WMI:获取所有串行(COM)端口(包括虚拟端口)的列表,c#,wmi,C#,Wmi,我目前正在开发一个小C#程序,与Arduino微控制器进行交互。程序有一个组合框,您可以在其中选择COM端口。µc通过USB和虚拟COM端口(CH340)连接 我使用下面的代码填充combobox的可用COM端口 private void Form1_Load(object sender, EventArgs e) { string[] PortNames = SerialPort. GetPortNames(); comboBoxPort.Items.A

我目前正在开发一个小C#程序,与Arduino微控制器进行交互。程序有一个组合框,您可以在其中选择COM端口。µc通过USB和虚拟COM端口(CH340)连接

我使用下面的代码填充combobox的可用COM端口

private void Form1_Load(object sender, EventArgs e)
    {
        string[] PortNames = SerialPort. GetPortNames();
        comboBoxPort.Items.AddRange(PortNames);
    }
这样做的缺点是,您必须查看设备管理器,以确定哪一个是适用于µc的正确管理器。例如,我的电脑有4个活动COM端口,一个物理端口,2个虚拟端口,另一个虚拟端口用于µc。我搜索的是一种显示设备完整名称和相关COM端口的方法(就像您可以在设备管理器中找到的那样)

经过一点研究,我发现使用WMI还有另一种可能性。在使用“WMI代码创建者”进行了大量测试之后,我不知道我还可以尝试做些什么。我尝试过的所有名称空间和类都只生成COM端口,如COM1、COM2……或者它们生成的硬件id对程序的用户不有用。 下面的代码或多或少正是我要搜索的,但它只适用于“真正的”内置COM端口

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
    {
        public class MyWMIQuery
        {
            public static void Main()
            {
                try
                {
                    ManagementObjectSearcher searcher =
                        new ManagementObjectSearcher("root\\CIMV2",
                        "SELECT * FROM Win32_SerialPort");

                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Win32_SerialPort instance");
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Name: {0}", queryObj["Name"]);
                    }
                }
                catch (ManagementException e)
                {
                    MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
                }
            }
        }
    }
是否有任何其他可能的方法来获取所有COM端口的列表,就像设备管理器的索引一样?是否可以使用设备的硬件id以某种方式识别它们,然后在第二步中为它们获取正确的名称

如果我能在这方面得到一些帮助,我将非常高兴。一定有办法,但我找不到


如果某些东西听起来很奇怪,请使用sry:)

如前所述,这里是完整的工作代码,用于在组合框中填充所有可用的COM端口,并在选择后设置相关的端口

(原始答案是如何获取端口名-->)

谢谢@o_o的链接,我希望有人会发现这段代码很有用

private void Form1_Load(object sender, EventArgs e)
{
    // Get all serial (COM)-ports you can see in the devicemanager
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\cimv2",
        "SELECT * FROM Win32_PnPEntity WHERE ClassGuid=\"{4d36e978-e325-11ce-bfc1-08002be10318}\"");

    // Sort the items in the combobox 
    CmdBoxPort.Sorted = true;

    // Add all available (COM)-ports to the combobox
    foreach (ManagementObject queryObj in searcher.Get())
        CmdBoxPort.Items.Add(queryObj["Caption"]);
}

private void CmdBoxPort_SelectedIndexChanged(object sender, EventArgs e)
{
    // Set the right port for the selected item.
    // The portname is based on the "COMx" part of the string (SelectedItem)
    string item = CmdBoxPort.SelectedItem.ToString();

    // Search for the expression "(COM" in the "selectedItem" string
    if (item.Contains("(COM"))
    {
        // Get the index number where "(COM" starts in the string
        int indexOfCom = item.IndexOf("(COM");

        // Set PortName to COMx based on the expression in the "selectedItem" string
        // It automatically gets the correct length of the COMx expression to make sure 
        // that also a COM10, COM11 and so on is working properly.
        serialPort1.PortName = item.Substring(indexOfCom + 1, item.Length - indexOfCom - 2);
    }
    else
        return;
}

Thx的答案,但这一个也只适用于“真实”物理COM端口,其他三个虚拟端口被忽略。我会在Windows注册表中搜索。这些信息一定在那里。对此我很抱歉,我希望我知道任何关于使用COM端口的知识,不管怎样,试试@Oscar说的。他似乎指向了正确的方向。@J.Schiller,如果
registry
search不是一个大热门的话。你也可以参考