[英]Get filenames without path of a specific directory
How can I get all filenames of a directory (and its subdirectorys) without the full path? 如何在没有完整路径的情况下获取目录(及其子目录)的所有文件名? Directory.GetFiles(...) returns always the full path! Directory.GetFiles(...)始终返回完整路径!
You can extract the filename from full path. 您可以从完整路径中提取文件名。
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");
Note that the *Files(dir, pattern, behavior)
methods can be simplified to non-recursive *Files(dir)
variants if subfolders aren't important 请注意,如果子文件夹不重要, *Files(dir, pattern, behavior)
方法可以简化为非递归*Files(dir)
变体
See Path.GetFileName : 请参见Path.GetFileName :
Returns the file name and extension of the specified path string. 返回指定路径字符串的文件名和扩展名。
The Path Class has several useful filename and path methods. Path类有几个有用的文件名和路径方法。
You want Path.GetFileName
你想要Path.GetFileName
This returns just the filename (with extension). 这只返回文件名(带扩展名)。
If you want just the name without the extension then use Path.GetFileNameWithoutExtension
如果您只想要没有扩展名的名称,请使用Path.GetFileNameWithoutExtension
You can just extract the file name from the full path. 您只需从完整路径中提取文件名即可。
var sections = fullPath.Split('\\');
var fileName = sections[sections.Length - 1];
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);
Although several right answers are there for this questions, You may find this solution as: 虽然这些问题有几个正确答案,但您可以找到以下解决方案:
string[] files = Directory.EnumerateFiles("C:\Something", "*.*")
.Select(p => Path.GetFileName(p))
.Where(s => s.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase) || s.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase)).ToArray();
Thanks 谢谢
Create a DirectoryInfo object, use a search pattern to enumerate, then treat it like an array. 创建一个DirectoryInfo对象,使用搜索模式进行枚举,然后将其视为数组。
string filePath = "c:\Public\";
DirectoryInfo apple = new DirectoryInfo(@filepath);
foreach (var file in apple.GetFiles("*")
{
//do the thing
Console.WriteLine(file)
}
You can get the files name of particular directory using GetFiles()
method of the DirectoryInfo
class. 您可以使用DirectoryInfo
类的GetFiles()
方法获取特定目录的文件名。 Here are sample example to list out all file and it's details of particular directory 以下是列出所有文件及其特定目录详细信息的示例
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());
This example display list of file in HTML table structure. 此示例显示HTML表结构中的文件列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.