如何在没有完整路径的情况下获取目录(及其子目录)的所有文件名? Directory.GetFiles(...)始终返回完整路径!
答案 0 :(得分:86)
您可以从完整路径中提取文件名。
var filenames3 = Directory
.GetFiles(dirPath, "*", SearchOption.AllDirectories)
.Select(f => Path.GetFileName(f));
var filenames4 = Directory
.EnumerateFiles(dirPath, "*", SearchOption.AllDirectories)
.Select(Path.GetFileName); // <-- note you can shorten the lambda
// - 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));
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)
方法可以简化为非递归*Files(dir)
变体
答案 1 :(得分:17)
答案 2 :(得分:8)
答案 3 :(得分:5)
您只需从完整路径中提取文件名即可。
var sections = fullPath.Split('\\');
var fileName = sections[sections.Length - 1];
答案 4 :(得分:3)
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);
答案 5 :(得分:2)
虽然有几个正确答案可以解决这个问题,但您可以找到以下解决方案:
string[] files = Directory.EnumerateFiles("C:\Something", "*.*")
.Select(p => Path.GetFileName(p))
.Where(s => s.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase) || s.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase)).ToArray();
由于
答案 6 :(得分:1)
创建一个DirectoryInfo对象,使用搜索模式进行枚举,然后将其视为数组。
string filePath = "c:\Public\";
DirectoryInfo apple = new DirectoryInfo(@filepath);
foreach (var file in apple.GetFiles("*")
{
//do the thing
Console.WriteLine(file)
}
答案 7 :(得分:-1)
您可以使用GetFiles()
类的DirectoryInfo
方法获取特定目录的文件名。
以下是列出所有文件的示例示例及其特定目录的详细信息
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表结构中的文件列表。