Get the path of a file listed in a combo list

0

I wanted to know if it is possible to obtain the path of a file which is listed in a combo, I mean, I've put the following code to list the name of the files in a folder in a combo:

DirectoryInfo d = new DirectoryInfo("1.76");
FileInfo[] files = d.GetFiles("*.bin");
comboBoxEdit1.Properties.Items.Clear();
comboBoxEdit1.Properties.Items.AddRange(files);

And now my question is, can I get the address of that file?

I would not use a case because I want to be able to introduce new files.

Try doing it comboBoxEdit1.SelectedIndex. in this way but I ran out of ideas on how to do the rest.

Another thing I think is to get the name of the selected object and make a comparison with the list of files in the folder and if it matches one get that address from that file and that it does in a variable, the problem is that I have the idea but I would not know how to do the code.

I think what I'm asking is impossible to do, but if someone knows how to do it, I'd really appreciate it if you could explain how, thanks.

    
asked by AlFaMoDz 30.12.2017 в 20:16
source

2 answers

0

If you are using the ComboBox that comes by default in Visual Studio to work with Windows Forms, that is, the one included in the .NET Framework: System.Windows.Forms.ComboBox , it does not have a Properties property. I suppose it will be an error to copy the load code of the ComboBox. It should be:

DirectoryInfo d = new DirectoryInfo(@"C:\Windows");
FileInfo[] files = d.GetFiles("*.bin");
comboBoxEdit1.Items.Clear();
comboBoxEdit1.Items.AddRange(files);

If you load the ComboBox in this way, the SelectedItem property of this will return a FileInfo object with the information of the selected element. Through the property FullName of the object FileInfo you can access the full path of the file:

private void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
{
    var file = (FileInfo)comboBoxEdit1.SelectedItem;
    Debug.WriteLine(file.FullName);
}
    
answered by 31.12.2017 / 12:42
source
0

Use the Datasource property

 DirectoryInfo d = new DirectoryInfo("1.76");
        FileInfo[] files = d.GetFiles("*.bin");
        comboBoxEdit1.DataSource = files;
        comboBoxEdit1.DisplayMember=nameof(FileInfo.Name);

The selected object will be a FileInfo, with all its properties

    
answered by 03.01.2018 в 08:45