我有以下代码:
string[] files = Directory.GetFiles(@"C:\Notes", "*.txt", SearchOption.TopDirectoryOnly);
foreach(string file in files)
当我检查文件内容时,它包含目录路径和扩展名。 有没有办法只获取文件名?我有以下代码:
string[] files = Directory.GetFiles(@"C:\Notes", "*.txt", SearchOption.TopDirectoryOnly);
foreach(string file in files)
当我检查文件内容时,它包含目录路径和扩展名。 有没有办法只获取文件名?Path.GetFileName
慢大约11倍。使用两者的正确时间是有的,但现在不应该使用 FileInfo
。 - Oskar KjellinPath.GetFileName(file)
System.IO.FileInfo f = new System.IO.FileInfo(@"C:\pagefile.sys"); // Sample file.
System.Windows.Forms.MessageBox.Show(f.FullName); // With extension.
System.Windows.Forms.MessageBox.Show(System.IO.Path.GetFileNameWithoutExtension(f.FullName)); // What you wants.
FileInfo.FullName
会给你完整的文件路径,这不是问题所要求的。其次,如果你要使用System.IO.Path
方法,为什么要在第一次创建一个FileInfo
对象呢? - Peter Monks