从 C# 中的完整文件路径解析目录名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/660657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse directory name from a full filepath in C#
提问by leora
If I have a string variable that has:
如果我有一个字符串变量,它具有:
"C:\temp\temp2\foo\bar.txt"
and I want to get
我想得到
"foo"
“富”
what is the best way to do this?
做这个的最好方式是什么?
采纳答案by Jon Skeet
Use:
用:
new FileInfo(@"C:\temp\temp2\foo\bar.txt").Directory.Name
回答by Handleman
Far be it for me to disagree with the Skeet, but I've always used
我绝对不同意飞碟,但我一直使用
Path.GetFileNameWithoutExtension(@"C:\temp\temp2\foo\bar.txt")
I suspect that FileInfo actually touches the file system to get it's info, where as I'd expect that GetFileNameWithoutExtension is only string operations - so performance of one over the other might be better.
我怀疑 FileInfo 实际上接触了文件系统以获取它的信息,正如我所期望的那样 GetFileNameWithoutExtension 只是字符串操作 - 因此一个比另一个更好的性能。
回答by Shameegh Boer
I had an occasion when I was looping through parent child directories
我有一次循环遍历父子目录
string[] years = Directory.GetDirectories(ROOT);
foreach (var year in years)
{
DirectoryInfo info = new DirectoryInfo(year);
Console.WriteLine(info.Name);
Console.WriteLine(year);
//Month directories
string[] months = Directory.GetDirectories(year);
foreach (var month in months)
{
Console.WriteLine(month);
//Day directories
string[] days = Directory.GetDirectories(month);
foreach (var day in days)
{
//checkes the files in the days
Console.WriteLine(day);
string[] files = Directory.GetFiles(day);
foreach (var file in files)
{
Console.WriteLine(file);
}
}
}
}
using this line I was able to get only the current directory name
使用这一行我只能获得当前目录名称
DirectoryInfo info = new DirectoryInfo(year);
Console.WriteLine(info.Name);
回答by rysama
It'll depend on how you want to handle the data, but another option is to use String.Split.
这将取决于您希望如何处理数据,但另一种选择是使用 String.Split。
string myStr = @"C:\foo\bar.txt";
string[] paths = myStr.Split('\');
string dir = paths[paths.Length - 2]; //returns "foo"
This doesn't check for an array out of bounds exception, but you get the idea.
这不会检查数组越界异常,但您明白了。
回答by Ananth
I think most simple solution is
我认为最简单的解决方案是
DirectoryInfo dinfo = new DirectoryInfo(path); string folderName= dinfo.Parent.Name;
回答by Chris J
Building on Handleman's suggestion, you can do:
根据 Handleman 的建议,您可以执行以下操作:
Path.GetFileName(Path.GetDirectoryName(path))
This doesn't touch the filesystem (unlike FileInfo
), and will do what's required. This will work with folders because, as the MSDNsays:
这不会触及文件系统(与 不同FileInfo
),并且会执行所需的操作。这将适用于文件夹,因为正如MSDN所说:
Return value:The characters after the last directory character in path. If the last character of path is a directory or volume separator character, this method returns String.Empty. If path is null, this method returns null.
返回值:path 中最后一个目录字符后的字符。如果路径的最后一个字符是目录或卷分隔符,则此方法返回 String.Empty。如果 path 为 null,则此方法返回 null。
Also, looking at the reference sourceconfirms that GetFilename
doesn't care if the path passed in is a file or a folder: it's just doing pure string manipulation.
此外,查看参考源确认GetFilename
并不关心传入的路径是文件还是文件夹:它只是在进行纯字符串操作。