Path.Combine absolute with relative path strings

path.combine c# not working
c# path.combine relative path
c# path.combine forward slash
path.join c#
path.combine unity
path combine relative url
c# path.combine for url
c combine path and filename

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

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

Path.Combine Method (System.IO), Combine absolute with relative path strings. Posted by: admin November 25, 2017 Leave a comment. Questions: I'm trying to join a Windows path with a relative  However unable to convert from absolute to relative path. First, I try to get the absolute path of the exe file. Which is successful. Second, get only directory of the exe file. Which is successful. Third, I am trying to combine the directory with the relative path of the Help.txt file. Which is unsuccessful.

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)

Path.Combine absolute with relative path strings, txt. Fortunately the Path class's GetFullPath method resolves a path that contains relative elements such as this one and returns an absolute path. Rather than keeping everything string-based, you should use a class which is designed to represent a file system path. If you're using Java 7 or Java 8, you should strongly consider using java.nio.file.Path; Path.resolve can be used to combine one path with another, or with a string.


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

Combine and resolve relative paths in C#, Combine (string path1, string path2) method and the Uri constructor Uri (Uri when the second parameter (the relative path) is an absolute path. Combines an array of strings into a path. This method assumes that the first argument is an absolute path and that the following argument or arguments are relative paths. If this is not the case, and particularly if any subsequent arguments are strings input by the user, call the Join or TryJoin

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;

Path.Combine and Uri violate Principle of least suprise · Issue , IO; class Program { static void Main() { string path = @"C:\programs\file.txt"; // Get file Combine handles certain cases where we have directory separators in It is ideal for file names, directory names, relative paths and file name extensions. Concatenates two strings that represent properly formed paths into one path; also concatenates any relative path elements. Note Misuse of this function can lead to a buffer overrun. We recommend the use of the safer PathCchCombine or PathCchCombineEx function in its place.

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

C# Path Examples, My guess for way it works this way for combining default paths with absolute/​relative paths: Document.SaveAs(Path.Combine(MyDocuments,  Rather than keeping everything string-based, you should use a class which is designed to represent a file system path. If you’re using Java 7 or Java 8, you should strongly consider using java.nio.file.Path; Path.resolve can be used to combine one path with another, or with a string.

TIL System.IO.Path.Combine will root your path if you pass a rooted , NET provides in its System.IO namespace the Path class which performs operations on String instances that contain file or directory path  When the first string is an fully qualified path that includes a drive and root directory and the second is a relative path from the first path, the two methods produce identical results. In the second and third calls to the ShowPathInformation method, the strings returned by the two methods diverge.

C# tip: Use Path Combine for file or directory path information , How do I find the relative path of a file in Visual Studio? Fortunately the Path class’s GetFullPath method resolves a path that contains relative elements such as this one and returns an absolute path. The following code shows how the program combines two paths that you enter. txtResult.Text = Path.GetFullPath( Path.Combine(txtPath1.Text, txtPath2.Text));

Why does Path.Combine not properly concatenate filenames that , , you will be prompted to filter that list first. The file system have the Path.Combine method to combine paths but how to combine a base URL with an absolute or relative URL/URI? The answer is to use the System.Uri-constructor to combine the URL: public static Uri CombineUri(string baseUri, string relativeOrAbsoluteUri) {return new Uri(new Uri(baseUri), relativeOrAbsoluteUri);}

Comments
  • Duplicate: stackoverflow.com/questions/623333/…
  • We're getting different answers here.. I don't think it's a duplicate
  • it's duplicate, although i think Path.GetFullName is a better solution.
  • You just contradicted yourself. But thanks for the alternate answer.
  • possible duplicate of Path.Combine and the dot notation
  • Yes, that is what I am insinuating with the post
  • 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))
  • Shouldn't the result of your What Works section be C:\bling.txt?
  • 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).
  • Note that this only works if the first path is an absolute path. It doesn't work for Path.GetFullPath(Path.Combine(@"..\..\blah",@"\bling"))
  • Note: should result in c:\windows\system32
  • 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)