如何用.txt文件填充我的组合框,但只有他们的名字没有路径?

Max*_*xim 4 c# combobox file

string path = AppDomain.CurrentDomain.BaseDirectory;
string[] filePaths = Directory.GetFiles(path, "*.txt");
foreach (string file in filePaths)
{
    cboLanden.Items.Add(file);
}
Run Code Online (Sandbox Code Playgroud)

这是我的代码,它返回完整路径,我想只有名称,没有我的组合框中的路径.

谢谢!

Ser*_*kiy 8

使用Path.GetFileName()获取文件名不带路径:

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

还要考虑使用文件作为comboBox的数据源:

 cboLanden.DataSource = Directory.EnumerateFiles(path, "*.txt")
                                 .Select(Path.GetFileName)
                                 .ToList();
Run Code Online (Sandbox Code Playgroud)