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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With