【问题标题】:How to get the second to last directory in a path string in C#如何在C#中获取路径字符串中的倒数第二个目录
【发布时间】:2012-12-05 16:10:45
【问题描述】:

例如,

string path = @"C:\User\Desktop\Drop\images\";

我只需要得到@"C:\User\Desktop\Drop\

有什么简单的方法吗?

【问题讨论】:

    标签: c# string path directory


    【解决方案1】:

    您可以使用Path 和Directory 类:

    DirectoryInfo parentDir = Directory.GetParent(Path.GetDirectoryName(path));
    string parent = parentDir.FullName; 
    

    请注意,如果路径不以目录分隔符字符 \ 结尾,您会得到不同的结果。那么images 会被理解为文件名而不是目录。

    你也可以使用Path.GetDirectoryName的后续调用

    string parent = Path.GetDirectoryName(Path.GetDirectoryName(path));
    

    此行为记录在 here:

    因为返回的路径不包含 DirectorySeparatorChar 或 AltDirectorySeparatorChar,将返回的路径传递回 GetDirectoryName 方法会导致一个文件夹被截断 每次后续调用结果字符串的级别。 例如,传递 路径“C:\Directory\SubDirectory\test.txt”进入 GetDirectoryName 方法将返回“C:\Directory\SubDirectory”。 将该字符串“C:\Directory\SubDirectory”传递到 GetDirectoryName 将产生“C:\Directory”。

    【讨论】:

    • +1 是唯一使用 Directory.GetParent 的解决方案,比字符串操作更安全、更好的方法
    • 在给定的示例中,此 Path.GetDirectoryName(Path.GetDirectoryName(path)) 将返回所需的结果,具体取决于尾部斜杠的存在,GetDirectoryName 将返回当前级别或父级别
    • 只要人们知道来自 GetDirectoryName 的不同结果,这应该涵盖这个问题 - 赞成。对于更复杂的场景,我添加了我自己的答案,带有斜杠检查
    【解决方案2】:
    var parent = ""; 
    If(path.EndsWith(System.IO.Path.DirectorySeparatorChar) || path.EndsWith(System.IO.Path.AltDirectorySeparatorChar))
    {
      parent = Path.GetDirectoryName(Path.GetDirectoryName(path));
      parent = Directory.GetParent(Path.GetDirectoryName(path)).FullName;
    }
    else
      parent = Path.GetDirectoryName(path);
    

    正如我所评论的,GetDirectoryName 是自我折叠的,它返回路径而不使用斜杠 - 允许获取下一个目录。使用 Directory.GetParent 进行 then clouse 也是有效的。

    【讨论】:

      【解决方案3】:

      简答:)

      path = Directory.GetParent(Directory.GetParent(path)).ToString();
      

      【讨论】:

        【解决方案4】:

        【讨论】:

          【解决方案5】:
          using System;
          
          namespace Programs
          {
              public class Program
              {      
                  public static void Main(string[] args)
                  {
                      string inputText = @"C:\User\Desktop\Drop\images\";
                      Console.WriteLine(inputText.Substring(0, 21));
                  }
              }
          }
          

          输出:

          C:\User\Desktop\Drop\

          【讨论】:

          • 不,但在这种情况下不是解决方案吗?我们没有其他信息包括那个。
          • 在一个非常具体的案例中 --- 但是什么时候编写好的软件是一次硬编码一个问题?
          • 当然。您是对的,但是当我查看问题时,我的答案看起来不错。当然这个问题不好,但我的回答是针对提问者的解决方案。但是我当然要再说一遍,你是对的。
          【解决方案6】:

          这将返回 "C:\User\Desktop\Drop\" 例如除了最后一个子目录之外的所有内容

          string path = @"C:\User\Desktop\Drop\images";
          string sub = path.Substring(0, path.LastIndexOf(@"\") + 1);
          

          如果你有一个斜杠,另一种解决方案:

          string path = @"C:\User\Desktop\Drop\images\";
          var splitedPath = path.Split('\\');
          var output = String.Join(@"\", splitedPath.Take(splitedPath.Length - 2));
          

          【讨论】:

            【解决方案7】:

            使用 File 或 Path 类可能有一些简单的方法可以做到这一点,但您也可以通过执行以下操作来解决它(注意:未经测试):

            string fullPath = "C:\User\Desktop\Drop\images\";
            string[] allDirs = fullPath.split(System.IO.Path.PathSeparator);
            
            string lastDir = allDirs[(allDirs.length - 1)];
            string secondToLastDir= allDirs[(allDirs.length - 2)];
            // etc...
            

            【讨论】:

              猜你喜欢
              • 2015-04-05
              • 1970-01-01
              • 1970-01-01
              • 2016-09-25
              • 1970-01-01
              • 1970-01-01
              • 2013-11-15
              • 2022-01-23
              • 1970-01-01
              相关资源
              最近更新 更多