Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I open the path of an item in a combobox while also only displaying the file name?

I am searching for exe files in a folder and displaying the file names in a combobox to be selected by the user. When the user presses a button the file selected should be opened. This works if I display the file path rather than the file name in the combobox.

public void Form1_Load(object sender, EventArgs e)
{

    string[] filePaths = Directory.GetFiles(@"C:\myFolder\", "*.exe");
    foreach (string file in filePaths)
    {
        comboBox1.Items.Add(Path.GetFileNameWithoutExtension(file));
    }
}

private void button1_Click(object sender, EventArgs e)
{
    object b = comboBox1.SelectedItem;
    string be = Convert.ToString(b);
    System.Diagnostics.Process.Start(be);
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For the program to be executed you need the entire path. For the display it should suffice to give the user a name for orientation. Now the only thing left is to map one to the other. For this you could use a simple dictionary. Collect all names as keys and the corresponding paths as values. Display only the names.

private Dictionary<string, string> map = new Dictionary<string, string>();

public void Form1_Load(object sender, EventArgs e)
{

    string[] filePaths = Directory.GetFiles(@"C:\myFolder\", "*.exe");
    foreach (string fullPath in filePaths)
    {
        string displayName = Path.GetFileNameWithoutExtension(fullPath);
        map.Add(displayName, fullPath);
        comboBox1.Items.Add(displayName);
    }
}

Then retrieve the path from the dictionary using the name as the key

private void button1_Click(object sender, EventArgs e)
{
    string nameKey = comboBox1.SelectedItem.ToString();
    string executablePath = map[nameKey];
    System.Diagnostics.Process.Start(executablePath);
}

Edit:

As suggested by Panagiotis Binding example:

To use binding you would need to specify what is displayed and what is treated as value in the combobox. You do that by telling the DisplayMember and ValueMember which property of the dictionary it should use. Furthermore the dictionary has to be encompassed by a BindingSource.

public void Form1_Load(object sender, EventArgs e)
{

    string[] filePaths = Directory.GetFiles(@"C:\myFolder\", "*.exe");
    foreach (string fullPath in filePaths)
    {
        string displayName = Path.GetFileNameWithoutExtension(fullPath);
        map.Add(displayName, fullPath);
    }

    comboBox1.DisplayMember="Key";
    comboBox1.ValueMember="Value";
    comboBox1.DataSource= new BindingSource(map, null);
}

In this example you would need to use the SelectedValue propertyto retrieve the value of the dictionary item, hence the full path. You can use it directly

private void button1_Click(object sender, EventArgs e)
{        
    System.Diagnostics.Process.Start(comboBox1.SelectedValue.ToString());
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading