C# 从完整路径获取目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/670819/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 12:42:47  来源:igfitidea点击:

get directory from full path

c#directory

提问by leora

If i have:

如果我有:

C:\temp\foo\bar\

C:\temp\foo\bar\

(NOTE:bar is a directory)

注意:bar 是一个目录)

how can i parse out:

我该如何解析:

bar

酒吧

采纳答案by leora

I figured it out.

我想到了。

DirectoryInfo info = new DirectoryInfo(sourceDirectory_);
string currentDirectoryName = info.Name;

回答by MrTelly

In Unix this is known as the basename, a quick google came up with this link for a C# version. I'm sure there are others ...

在 Unix 中这被称为 basename,一个快速的谷歌想出了这个C# 版本的链接。我确定还有其他人......

回答by Program.X

Try

尝试

System.IO.Path.GetFileName("C:\temp\foo\bar");

回答by Wim Haanstra

if the answers above do not satisfy your needs, why not just substring the string from the last .

如果上面的答案不能满足您的需求,为什么不从最后一个 .

string dirName = originalDirName.Substring(originalDirName.LastIndexOf("\") + 1);

sure, you should do some checking if the originalDirName does not end on a \ and if the originalDirName is longer than zero and actually contains \ characters.

当然,您应该检查 originalDirName 是否不以 \ 结尾,以及 originalDirName 是否长于零并且实际上包含 \ 字符。

回答by Chris S

I can think of 4 ways instantly

我能立刻想到4种方法

1

1

  • If the string ends with a slash remove it
  • Use Path.GetFilename (or numerous other System.IO methods)
  • 如果字符串以斜杠结尾,则将其删除
  • 使用 Path.GetFilename(或许多其他 System.IO 方法)

2

2

  • Split the string on slashes into an array
  • Get the last index of the array
  • 将斜杠上的字符串拆分为数组
  • 获取数组的最后一个索引

3

3

  • Create a Uri class with it in the constructor
  • Use the Segments property
  • 在构造函数中用它创建一个 Uri 类
  • 使用 Segments 属性

4

4

  • The linq way someone mentioned above
  • 上面提到的linq方式

回答by Daniel Earwicker

It looks like a bunch of people have withdrawn their answers, which is possibly a shame.

看起来一堆人撤回了他们的答案,这可能是一种耻辱。

This one's got to be worth stating, only for the "teach a man to fish" quality of it - it's short, elegant and made of two separate things that, once learned, can be re-applied to other problems.

值得一提的是,这只是因为它的“教人钓鱼”的品质——它简短、优雅,由两个独立的东西组成,一旦学会,就可以重新应用于其他问题。

string lastPiece = wholePath.Split('\').Last();

Lastwill throw if the list is empty.

Last如果列表为空,将抛出。

回答by Anuraj

Try this

尝试这个

string DirName = System.IO.Directory.GetParent(@"C:\temp\foo\bar\").Name;

回答by Norbert B.

Just use:

只需使用:

string dirname = new DirectoryInfo(@"C:\temp\foo\bar\").Name;      

According to MSDN this returns the name of the directory, not the full path.

根据 MSDN,这将返回目录的名称,而不是完整路径。

Link to MSDN Library

链接到 MSDN 库

Hope this helps.........

希望这可以帮助.........

回答by Matthew M.

The simplest way to do this withoutcreating a new DirectoryInfoinstance is to use the Path.GetFileNamestatic method. This is located in System.IO.

创建新DirectoryInfo实例的情况下执行此操作的最简单方法是使用Path.GetFileName静态方法。它位于 System.IO 中。

using System.IO;

string lastFolderName = Path.GetFileName(@"C:\Folder1\Folder2");

The variable would be set to "Folder2".

该变量将设置为“Folder2”。

This is quite a bit more efficientthat creating a new instance of the DirectoryInfo class!

这比创建 DirectoryInfo 类的新实例要高效得多

回答by Razvan

string dirname = new DirectoryInfo(path).Name;  
Console.WriteLine(dirname);