如何找到路径的父目录?

时间:2022-07-23 15:05:12

How do I get a parent of a directory, for example:

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

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

Output:  C:\AAA\BBB\CCC\

3 个解决方案

#1


29  

upDir = Directory.GetParent(path).FullName;

#2


7  

Everything you want is in the Directory class:

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

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

In particular, GetParent:

特别是,GetParent:

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

#3


6  

string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string parentDir = Directory.GetParent(path).FullName;

#1


29  

upDir = Directory.GetParent(path).FullName;

#2


7  

Everything you want is in the Directory class:

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

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

In particular, GetParent:

特别是,GetParent:

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

#3


6  

string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string parentDir = Directory.GetParent(path).FullName;