代码之家  ›  专栏  ›  技术社区  ›  ErocM

C取下1个目录[重复]

c#
  •  0
  • ErocM  · 技术社区  · 15 年前

    这个问题已经有了答案:

    我试过几种方法来达到这个目的,但现在…

    我有以下目录:

    C:\一\二\三

    我需要转到这个目录:

    C:一个二

    不总是只有3个目录,可能是这样的:

    C:\1\2\3\4\5

    我需要的是:

    C:\一\二\三\四

    总是有一个目录关闭…

    有人有什么建议吗?

    谢谢 厄洛克

    5 回复  |  直到 12 年前
        1
  •  6
  •   SLaks    15 年前
        2
  •  2
  •   Darin Dimitrov    15 年前

    你可以使用 Parent 性质 DirectoryInfo 或静态 GetParent 方法:

    DirectoryInfo parent = Directory.GetParent(@"c:\one\two\three\four\five");
    Console.WriteLine(parent.Name);
    
        3
  •  1
  •   Mark H    15 年前
    Path.Combine(directoryName, "..");
    
        4
  •  0
  •   Doggett    15 年前

    如果您对启动路径感兴趣,也可以使用appdomain.currentdomain.basedirectory或application.startup path(winforms)

        5
  •  0
  •   VMAtm    12 年前

    你可以使用 Directory.GetParent 方法,如下所示:

    Console.WriteLine(Directory.GetParent(@"c:\one\two\three\four\five").FullName);
    

    结果= & gt;

    c:\one\two\three\four