How can I fill my combobox with .txt files, but only their name without the path?

c#, combobox, file

Solution

Use `Path.GetFileName()` to get file name without path:

string path = AppDomain.CurrentDomain.BaseDirectory; 
string[] filePaths = Directory.GetFiles(path, "*.txt"); 
foreach (string file in filePaths) 
{ 
   cboLanden.Items.Add(Path.GetFileName(file)); 
}

Also consider to use files as data source of your comboBox:

 cboLanden.DataSource = Directory.EnumerateFiles(path, "*.txt")
                                 .Select(Path.GetFileName)
                                 .ToList();

Problem

``` string path = AppDomain.CurrentDomain.BaseDirectory; string[] filePaths = Directory.GetFiles(path, "*.txt"); foreach (string file in filePaths) { cboLanden.Items.Add(file); } ``` This is my code and it returns the full path, I would like to have only the name, without the path in my combobox.

Original source