友好的端口名称组合框c# Windows窗体

本文关键字:组合 Windows 窗体 | 更新日期: 2023-09-27 17:49:25

我有一个关于友好的名字的问题,我读了很多(真的很多)关于如何获得友好的名字。我甚至使用了WMICodeCreator,我有一个代码来放。但实际上我不明白如何将这个添加到我的组合框菜单。这是我的代码。实际上,这是显示计算机上可用的COM端口,但我想显示这个友好的名称,使我的应用程序更容易使用。我怎样才能显示友好的名称而不是"COMx"?我在c#上使用Windows窗体应用程序。

    public partial class Form1 : Form
{
    private SerialPort myport;
    public Form1()
    {
        InitializeComponent();
        myport = new SerialPort();
    }
    private void portbox_DropDown(object sender, EventArgs e)
    {
        string[] ports = SerialPort.GetPortNames();
        portbox.Items.Clear();
        foreach (string port in ports)
        {
           portbox.Items.Add(port);
        }
    }
    private void butvalidate_Click(object sender, EventArgs e)
    {
        myport.BaudRate = 9600;
        myport.PortName = portbox.SelectedItem.ToString();
        myport.Open();
    }

提前感谢您的帮助

友好的端口名称组合框c# Windows窗体

这是一个老问题,但是当我在搜索相关的东西时,它出现了,所以我将发布我用于此精确目的的代码。

我从SerialPort获得portName列表,然后将这些列表与从WMI查询中检索到的友好名称列表进行匹配,并将它们放入一个Dictionary中,其中portName作为键,友好名称作为值。然后我使用这个字典作为组合框的绑定源。还有其他方法可以做到这一点,但我觉得这种方法简单明了。

这个术语有点令人困惑,因为ComboBox有一个DisplayMember用来显示文本,还有一个ValueMember作为SelectedValue提供。DisplayMember是字典"Value",ValueMember是字典"Key"。"

    using System.Management;
    private void getAvailablePorts()
    {
        // Get friendly names to go along with the actual portNames
        using (var searcher = new ManagementObjectSearcher("SELECT * FROM WIN32_SERIALPORT"))
        {
           var portDictionary = new Dictionary<string, string>();
           var portNames = SerialPort.GetPortNames().OrderByDescending(s => s.GetAlphaNumericOrderToken()).ToArray<object>();
           var portList = searcher.Get().Cast<ManagementBaseObject>().ToList();
            foreach( var portName in portNames)
            {
                // WMI does not always find all com ports so provide a null alternative
                var portDesc = portList.Where(p => p["DeviceID"].ToString() == portName.ToString()).Select(q => q["Caption"].ToString()).FirstOrDefault() ?? portName.ToString();
                portDictionary.Add(portName.ToString(), portDesc);
            }
            portComboBox.DataSource = new BindingSource(portDictionary, null);
            portComboBox.DisplayMember = "Value";
            portComboBox.ValueMember = "Key";
            // I set my comboBox Selected entry to be the one with a friendly name 
            // beginning with "Teensy" (of course yours will be different, 
            // or you can leave this out)
            portComboBox.SelectedIndex = portComboBox.FindString("Teensy");  // -1 if not found
            if (portComboBox.SelectedIndex < 0)
                portComboBox.Selected = 0;
        }
    }

用法:

            // Since the ComboBox text is now different from the value, 
            // you have to specifically get the SelectedValue of the ComboBox 
            // rather than just its text. 
            var _port = new SerialPort(portComboBox.SelectedValue.ToString());

我希望这对某人有所帮助!