获取没有特定目录path的文件名

我怎样才能得到一个目录(及其子目录)的所有文件名没有完整的path? Directory.GetFiles(…)总是返回完整的path!

您可以从完整path中提取文件名。

.NET 3,只有文件名

var filenames3 = Directory .GetFiles(dirPath, "*", SearchOption.AllDirectories) .Select(f => Path.GetFileName(f)); 

.NET 4,文件名只

 var filenames4 = Directory .EnumerateFiles(dirPath, "*", SearchOption.AllDirectories) .Select(Path.GetFileName); // <-- note you can shorten the lambda 

返回目录内的相对path的文件名

 // - file1.txt // - file2.txt // - subfolder1/file3.txt // - subfolder2/file4.txt var skipDirectory = dirPath.Length; // because we don't want it to be prefixed by a slash // if dirPath like "C:\MyFolder", rather than "C:\MyFolder\" if(!dirPath.EndsWith("" + Path.DirectorySeparatorChar)) skipDirectory++; var filenames4s = Directory .EnumerateFiles(dirPath, "*", SearchOption.AllDirectories) .Select(f => f.Substring(skipDirectory)); 

在LinqPad确认…

 filenames3.SequenceEqual(filenames4).Dump(".NET 3 and 4 methods are the same?"); filenames3.Dump(".NET 3 Variant"); filenames4.Dump(".NET 4 Variant"); filenames4s.Dump(".NET 4, subfolders Variant"); 

请注意,如果子文件夹不重要, *Files(dir, pattern, behavior)方法可以简化为非recursion*Files(dir)变体

请参阅Path.GetFileName :

返回指定的pathstring的文件名和扩展名。

Path类有几个有用的文件名和path方法。

您可以从完整path中提取文件名称。

 var sections = fullPath.Split('\\'); var fileName = sections[sections.Length - 1]; 

你想要Path.GetFileName

这只返回文件名(带扩展名)。

如果你只想要没有扩展名的名字,那么使用Path.GetFileNameWithoutExtension

 string fileName = @"C:\mydir\myfile.ext"; string path = @"C:\mydir\"; string result; result = Path.GetFileName(fileName); Console.WriteLine("GetFileName('{0}') returns '{1}'", fileName, result); result = Path.GetFileName(path); Console.WriteLine("GetFileName('{0}') returns '{1}'", path, result); 

虽然这个问题有几个正确的答案,你可以find这个解决scheme:

 string[] files = Directory.EnumerateFiles("C:\Something", "*.*") .Select(p => Path.GetFileName(p)) .Where(s => s.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase) || s.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase)).ToArray(); 

谢谢

创build一个DirectoryInfo对象,使用search模式枚举,然后像一个数组。

 string filePath = "c:\Public\"; DirectoryInfo apple = new DirectoryInfo(@filepath); foreach (var file in apple.GetFiles("*") { //do the thing Console.WriteLine(file) } 

您可以使用DirectoryInfo类的GetFiles()方法获取特定目录的文件名。 下面是示例列出所有文件,它是特定目录的详细信息

 System.Text.StringBuilder objSB = new System.Text.StringBuilder(); System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo("d:\\"); objSB.Append("<table>"); objSB.Append("<tr><td>FileName</td>" + "<td>Last Access</td>" + "<td>Last Write</td>" + "<td>Attributes</td>" + "<td>Length(Byte)</td><td>Extension</td></tr>"); foreach (System.IO.FileInfo objFile in directory.GetFiles("*.*")) { objSB.Append("<tr>"); objSB.Append("<td>"); objSB.Append(objFile.Name); objSB.Append("</td>"); objSB.Append("<td>"); objSB.Append(objFile.LastAccessTime); objSB.Append("</td>"); objSB.Append("<td>"); objSB.Append(objFile.LastWriteTime); objSB.Append("</td>"); objSB.Append("<td>"); objSB.Append(objFile.Attributes); objSB.Append("</td>"); objSB.Append("<td>"); objSB.Append(objFile.Length); objSB.Append("</td>"); objSB.Append("<td>"); objSB.Append(objFile.Extension); objSB.Append("</td>"); objSB.Append("</tr>"); } objSB.Append("</table>"); Response.Write(objSB.ToString()); 

这个例子显示HTML表格结构中的文件列表。