How do I find the parent directory of a path?

后端 未结 4 1965
悲哀的现实
悲哀的现实 2020-12-06 15:57

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

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

Output:  C:\\AAA\\BBB\\CCC\\
相关标签:
4条回答
  • 2020-12-06 16:36
    string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    string parentDir = Directory.GetParent(path).FullName;
    
    0 讨论(0)
  • 2020-12-06 16:40
    var upDir = new DirectoryInfo(yourPath).Parent.FullName;
    
    0 讨论(0)
  • 2020-12-06 16:55
    upDir = Directory.GetParent(path).FullName;
    
    0 讨论(0)
  • 2020-12-06 16:55

    Everything you want is in the Directory class:

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

    In particular, GetParent:

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

    0 讨论(0)
提交回复
热议问题