21

如何获取目录的父级,例如:

string upDir = GetOneLvlUp(@"C:\AAA\BBB\CCC\DDD\");

Output:  C:\AAA\BBB\CCC\
4

4 回答 4

41
upDir = Directory.GetParent(path).FullName;
于 2012-07-06T13:38:21.823 回答
7

您想要的一切都在 Directory 类中:

http://msdn.microsoft.com/en-us/library/system.io.directory.aspx

特别是,GetParent:

http://msdn.microsoft.com/en-us/library/system.io.directory.getparent.aspx

于 2012-07-06T13:37:39.027 回答
6
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string parentDir = Directory.GetParent(path).FullName;
于 2016-01-29T06:42:48.543 回答
0
var upDir = new DirectoryInfo(yourPath).Parent.FullName;
于 2019-06-17T09:28:13.383 回答