Path.Combine absolute with relative path strings

117,175

Solution 1

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")

Solution 2

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)

Solution 3


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

Solution 4

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

Solution 5

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;
Share:
117,175

Related videos on Youtube

CVertex
Author by

CVertex

"There's a risk I'll OD on imagination like my friend Chauncey" - Wonder Showzen.

Updated on July 08, 2022

Comments

  • CVertex
    CVertex almost 2 years

    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)?

    • CVertex
      CVertex about 15 years
      We're getting different answers here.. I don't think it's a duplicate
    • Greg Dean
      Greg Dean about 15 years
      it's duplicate, although i think Path.GetFullName is a better solution.
    • CVertex
      CVertex about 15 years
      You just contradicted yourself. But thanks for the alternate answer.
    • Julien Bérubé
      Julien Bérubé almost 10 years
      possible duplicate of Path.Combine and the dot notation
  • Llyle
    Llyle over 14 years
    Yes, that is what I am insinuating with the post
  • Nelson Rothermel
    Nelson Rothermel almost 11 years
    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))
  • cod3monk3y
    cod3monk3y over 9 years
    Note: should result in c:\windows\system32
  • cod3monk3y
    cod3monk3y over 9 years
    Shouldn't the result of your What Works section be C:\bling.txt?
  • Paul Williams
    Paul Williams over 9 years
    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)
  • F-H
    F-H over 6 years
    Why does the URI-based method not work? According to this answer, the result is valid (and it seems to be recognized on Windows, as well).
  • derekantrican
    derekantrican about 4 years
    Note that this only works if the first path is an absolute path. It doesn't work for Path.GetFullPath(Path.Combine(@"..\..\blah",@"\bling"))