C# 如何用.txt文件填充组合框,但只填充其名称而不填充路径?

C# 如何用.txt文件填充组合框,但只填充其名称而不填充路径?,c#,file,combobox,C#,File,Combobox,这是我的代码,它返回完整的路径,我希望在我的组合框中只有名称,没有路径。用于获取没有路径的文件名: string path = AppDomain.CurrentDomain.BaseDirectory; string[] filePaths = Directory.GetFiles(path, "*.txt"); foreach (string file in filePaths) { cboLanden.Items.Add(file); } 也考虑使用文件作为组合框的数据源: st

这是我的代码,它返回完整的路径,我希望在我的组合框中只有名称,没有路径。

用于获取没有路径的文件名:

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

也考虑使用文件作为组合框的数据源:

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

 cboLanden.DataSource = Directory.EnumerateFiles(path, "*.txt")
                                 .Select(Path.GetFileName)
                                 .ToList();
如果像
c:\coolpic.jpg这样的文件中有文件名

它将只返回
coolpic
而不带扩展名

错误1“Path”是Path.GetFileName上“System.IO.Path”和“System.Windows.Shapes.Path”之间的不明确引用错误?我已经添加了using.windows.shape,但@user3173605仍然使用完全限定名,如
System.IO.Path.GetFileName()
谢谢您的回答!
foreach (string file in files) 
{ 
  Path.GetFileNameWithoutExtension(file);
}