123

I'm trying to join a Windows path with a relative path using Path.Combine.

However, Path.Combine(@"C:\blah",@"..\bling") returns C:\blah\..\bling instead of C:\bling\.

Does anyone know how to accomplish this without writing my own relative path resolver (which shouldn't be too hard)?

Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
CVertex
  • 17,997
  • 28
  • 94
  • 124

8 Answers8

87

What Works:

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

(result: absolutePath="C:\bling.txt")

What doesn't work

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

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

Cyral
  • 13,999
  • 6
  • 50
  • 90
Llyle
  • 5,980
  • 6
  • 39
  • 56
  • 8
    Yes, that is what I am insinuating with the post – Llyle Oct 28 '09 at 19:32
  • 9
    Just make sure baseDirectory has the trailing \\, otherwise you end up with `C:\\blah..\\bling.txt` and that doesn't work. In that case you can manually add them to the string or do `Path.GetFullPath(Path.Combine(baseDirectory, relativePath))` – Nelson Rothermel Jun 06 '13 at 21:15
  • 5
    Shouldn't the result of your __What Works__ section be `C:\bling.txt`? – cod3monk3y Aug 13 '14 at 17:45
  • Why does the URI-based method not work? According to [this answer](https://stackoverflow.com/a/46629990/5206621), the result is valid (and it [seems to be recognized on Windows, as well](https://stackoverflow.com/a/38428899/5206621)). – F-H Oct 08 '17 at 10:32
50

Call Path.GetFullPath on the combined path http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx

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

(I agree Path.Combine ought to do this by itself)

Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
  • 2
    Note that this only works if the first path is an absolute path. It doesn't work for `Path.GetFullPath(Path.Combine(@"..\..\blah",@"\bling"))` – derekantrican Apr 01 '20 at 01:49
19

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

shahkalpesh
  • 33,172
  • 3
  • 63
  • 88
5

For windows universal apps Path.GetFullPath() is not available, you can use the System.Uri class instead:

 Uri uri = new Uri(Path.Combine(@"C:\blah\",@"..\bling"));
 Console.WriteLine(uri.LocalPath);
thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
5

Path.GetFullPath() does not work with relative paths.

Here's the solution that works with both relative + absolute paths. It works on both Linux + Windows and it keeps the .. as expected in the beginning of the text (at rest they will be normalized). The solution still relies on Path.GetFullPath to do the fix with a small workaround.

It's an extension method so use it like 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));
}
U. Bulle
  • 1,075
  • 9
  • 20
4

This will give you exactly what you need (path does NOT have to exist for this to work)

DirectoryInfo di = new DirectoryInfo(@"C:\blah\..\bling");
string cleanPath = di.FullName;
  • 1
    Both Path.GetFullPath() and DirectoryInfo.FullName will work on a fictitious path. The problem is when the file actually exists, the executing process needs FileIOPermission - true for both APIs. (see MSDN) – Paul Williams Jan 30 '15 at 19:06
1

Be careful with Backslashes, don't forget them (neither use twice:)

string relativePath = "..\\bling.txt";
string baseDirectory = "C:\\blah\\";
//OR:
//string relativePath = "\\..\\bling.txt";
//string baseDirectory = "C:\\blah";
//THEN
string absolutePath = Path.GetFullPath(baseDirectory + relativePath);
zaknafein
  • 11
  • 1
  • There's no problem with any of the answers nor the original question, as they use the "@", we don't have to double backslashes – Elo Mar 28 '23 at 08:45
0

To handle any absolute, relative, or URI base paths, it doesn't seem like there's one canned solution that fits all... so, I wrote one:

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

Feel free to improve performance or apply whatever shortcuts apply to my answer (I don't usually work in C#; I usually write C++).