2

I have a question about friendly name I read a lot (really a lot) about how to get the friendly name. I even used the WMICodeCreator I have a code to put. But actually I don't understand how to add this to my combobox menu. here is my code. Actually this one is showing the available COM port on the computer, but I would like to show the friendlyname of this one to make my application easier to use. How can I do to display friendlyname instead of "COMx"? I'm using Windows Form application on C#.

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

In advance thanks for your help

powered
  • 21
  • 2
  • Try [this](http://stackoverflow.com/questions/9370859/get-friendly-port-name-programatically) – TaW May 17 '15 at 18:06
  • I aldready used all that pages, all the codes, tried to add a lot of lines, moving, changing all compacting all I could read and nothing do what I want it's full of error message. especially with the `ManagementObjectSearcher` – powered May 17 '15 at 19:14

1 Answers1

0

This is an old question, but it came up when I was searching for something related so I will post the code that I use for this precise purpose.

I get the list of portNames from SerialPort, then match those to the list of friendly names retrieved from a WMI query, and put them into a Dictionary with the portName as the key and the friendly name as the value. Then I use this Dictionary as the BindingSource for the ComboBox. There are other ways to do this but I find this approach simple and clear.

This terminology is a bit confusing because the ComboBox has a DisplayMember that it will use to display text and a ValueMember that it will provide as the SelectedValue. The DisplayMember is the dictionary "Value" and the ValueMember is the dictionary "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;
        }
    }

Usage:

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

I hope this helps somebody!

Craig.Feied
  • 2,617
  • 2
  • 16
  • 25