Khaab Khaab -4 years ago 122
C# Question

ComboBox showing file names

I have created a comboBox inside a Windows Form and inside this comboBox I want to show filenames inside a specific directory.

My code Form 1:

private string path = (@"C:\Users\khaab\Documents\visual studio 2015\Projects\ReadingXML\ReadingXML\bin\Debug\Customers");

private void SelectConfigComboBox_DropDown(object sender, EventArgs e)
{
List<String> Configurations = Directory.EnumerateDirectories(path).ToList();
Path.GetFileName(path);
SelectConfigComboBox.DataSource = Configurations;
}


My problem at this moment is that when I click on the ComboBox it shows me the whole path name I want only the names of the file inside that directory.

Answer Source

after enumerating all files apply Path.GetFileName method to each of them using Select extension method:

private void SelectConfigComboBox_DropDown(object sender, EventArgs e)
{
   List<String> Configurations = Directory.EnumerateFiles(path)
                                          .Select(p => Path.GetFileName(p))
                                          .ToList();
   SelectConfigComboBox.DataSource = Configurations;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download