windows 路径,合并绝对路径字符串和相对路径字符串

3zwtqj6y  于 2023-03-24  发布在  Windows
关注(0)|答案(8)|浏览(371)

我正在尝试使用Path.Combine将Windows路径与相对路径连接。
但是,Path.Combine(@"C:\blah",@"..\bling")返回C:\blah\..\bling而不是C:\bling\
有没有人知道如何在不编写自己的相对路径解析器的情况下完成这一任务(这应该不会太难)?

iecba09b

iecba09b1#

有效:

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

(result:absolutePath=“C:\bling.txt”)

什么行不通

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

(result:absolutePath=“C:/blah/bling.txt”)

fwzugrvs

fwzugrvs2#

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

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

(我同意Path.合并应该自己做这件事)

2cmtqfgy

2cmtqfgy3#

Path.GetFullPath(@"c:\windows\temp\..\system32")?
ibrsph3r

ibrsph3r4#

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

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

ppcbkaq55#

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));
}
bfhwhh0e

bfhwhh0e6#

这将给予你你所需要的(路径并不一定存在,这工作)

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

dxxyhpgq7#

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

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

0vvn1miw8#

要处理任何绝对、相对或URI基本路径,似乎没有一个现成的解决方案可以满足所有需求......所以,我写了一个:

public static String CombinePaths(String basepath, String relpath)
    {
        Stack<String> vs = new Stack<String>();

        int i;

        var s = basepath.Split('\\');
        for (i = 0; i < s.Length; i++)
        {
            if (s[i] != "..")
            {
                if (s[i] != ".")
                    vs.Push(s[i]);
            }
            else
            {
                vs.Pop();
            }
        }

        var r = relpath.Split('\\');
        for (i = 0; i < r.Length; i++)
        {
            if (r[i] != "..")
            {
                if (r[i] != ".")
                    vs.Push(r[i]);
            }
            else
            {
                vs.Pop();
            }
        }

        String ret = "";
        var a = vs.ToArray();
        i = a.Count() - 1;
        while (i > 0)
        {
            ret += a[i].ToString();
            ret += "\\";
            i--;
        }
        ret += a[0].ToString();

        return ret;
    }

请随意提高性能或应用任何适用于我的答案的快捷方式(我通常不使用C#;我通常写C++。

相关问题