【问题标题】:Path.Combine absolute with relative path stringsPath.Combine absolute 与相对路径字符串
【发布时间】:2010-10-14 19:24:54
【问题描述】:

我正在尝试使用 Path.Combine 将 Windows 路径与相对路径连接起来。

但是,Path.Combine(@"C:\blah",@"..\bling") 返回 C:\blah\..\bling 而不是 C:\bling\

有谁知道如何在不编写我自己的相对路径解析器的情况下完成此操作(这应该不会太难)?

【问题讨论】:

  • 我们在这里得到不同的答案.. 我不认为这是重复的
  • 它是重复的,虽然我认为 Path.GetFullName 是一个更好的解决方案。
  • 你只是自相矛盾。但感谢您的替代答案。

标签: c# .net windows path filesystems


【解决方案1】:
Path.GetFullPath(@"c:\windows\temp\..\system32")?

【讨论】:

  • 注意:应该是c:\windows\system32
【解决方案2】:

什么有效:

string relativePath = "..\\bling.txt";
string baseDirectory = "C:\\blah\\";
string absolutePath = Path.GetFullPath(baseDirectory + relativePath);

(结果:absolutePath="C:\bling.txt")

什么不起作用

string relativePath = "..\\bling.txt";
Uri baseAbsoluteUri = new Uri("C:\\blah\\");
string absolutePath = new Uri(baseAbsoluteUri, relativePath).AbsolutePath;

(结果:absolutePath="C:/blah/bling.txt")

【讨论】:

  • 是的,这就是我在帖子中暗示的内容
  • 只要确保 baseDirectory 有尾随 \\,否则你最终会得到 C:\\blah..\\bling.txt,这是行不通的。在这种情况下,您可以手动将它们添加到字符串中或执行Path.GetFullPath(Path.Combine(baseDirectory, relativePath))
  • 您的 What Works 部分的结果不应该是C:\bling.txt吗?
  • 为什么基于URI的方法不起作用?根据this answer,结果是有效的(而且是seems to be recognized on Windows, as well)。
【解决方案3】:

在组合路径上调用 Path.GetFullPath http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx

> Path.GetFullPath(Path.Combine(@"C:\blah\",@"..\bling"))
C:\bling

(我同意 Path.Combine 应该自己做)

【讨论】:

  • 请注意,这仅在第一个路径是绝对路径时才有效。它不适用于Path.GetFullPath(Path.Combine(@"..\..\blah",@"\bling"))
【解决方案4】:

这将为您提供您所需要的(路径不必存在)

DirectoryInfo di = new DirectoryInfo(@"C:\blah\..\bling");
string cleanPath = di.FullName;

【讨论】:

  • Path.GetFullPath() 和 DirectoryInfo.FullName 都可以在虚构的路径上工作。问题是当文件实际存在时,执行进程需要 FileIOPermission - 两个 API 都为 true。 (见 MSDN)
【解决方案5】:

对于 Windows 通用应用程序 Path.GetFullPath() 不可用,您可以改用 System.Uri 类:

 Uri uri = new Uri(Path.Combine(@"C:\blah\",@"..\bling"));
 Console.WriteLine(uri.LocalPath);

【讨论】:

    【解决方案6】:

    小心使用反斜杠,不要忘记它们(不要使用两次:)

    string relativePath = "..\\bling.txt";
    string baseDirectory = "C:\\blah\\";
    //OR:
    //string relativePath = "\\..\\bling.txt";
    //string baseDirectory = "C:\\blah";
    //THEN
    string absolutePath = Path.GetFullPath(baseDirectory + relativePath);
    

    【讨论】:

      【解决方案7】:

      Path.GetFullPath() 不适用于相对路径。

      这是适用于相对 + 绝对路径的解决方案。它可以在 Linux + Windows 上运行,并且在文本开头保持.. 的预期(在休息时它们将被规范化)。该解决方案仍然依赖Path.GetFullPath 来通过一个小变通方法进行修复。

      这是一个扩展方法,所以像text.Canonicalize()一样使用它

      /// <summary>
      ///     Fixes "../.." etc
      /// </summary>
      public static string Canonicalize(this string path)
      {
          if (path.IsAbsolutePath())
              return Path.GetFullPath(path);
          var fakeRoot = Environment.CurrentDirectory; // Gives us a cross platform full path
          var combined = Path.Combine(fakeRoot, path);
          combined = Path.GetFullPath(combined);
          return combined.RelativeTo(fakeRoot);
      }
      private static bool IsAbsolutePath(this string path)
      {
          if (path == null) throw new ArgumentNullException(nameof(path));
          return
              Path.IsPathRooted(path)
              && !Path.GetPathRoot(path).Equals(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal)
              && !Path.GetPathRoot(path).Equals(Path.AltDirectorySeparatorChar.ToString(), StringComparison.Ordinal);
      }
      private static string RelativeTo(this string filespec, string folder)
      {
          var pathUri = new Uri(filespec);
          // Folders must end in a slash
          if (!folder.EndsWith(Path.DirectorySeparatorChar.ToString())) folder += Path.DirectorySeparatorChar;
          var folderUri = new Uri(folder);
          return Uri.UnescapeDataString(folderUri.MakeRelativeUri(pathUri).ToString()
              .Replace('/', Path.DirectorySeparatorChar));
      }
      

      【讨论】:

        猜你喜欢
        • 2013-04-17
        • 2022-08-17
        • 2011-06-27
        • 2014-10-29
        • 2020-05-18
        • 1970-01-01
        相关资源
        最近更新 更多