Relative path to absolute path in C#?

C#Relative PathAbsolute Path

C# Problem Overview


I have xml files that contain href file paths to images (e.g. "....\images\image.jpg"). The hrefs contain relative paths. Now, I need to extract the hrefs to the images and turn them into absolute paths in the file system.

I know about the GetFullPath method, but I tried it and it only seems to work from the CurrentDirectory set, which appears to be C: so I don't see how I could use that. And still, I have the absolute path of the file containing the hrefs, and the href relative paths, so since it is a simple task for me to count back the number of "...." parts based on the absolute path of the containing file, it seems there must be a way to do this programmatically as well.

I'm hoping there's some simple method I just don't know about! Any ideas?

C# Solutions


Solution 1 - C#

string exactPath = Path.GetFullPath(yourRelativePath);

works

Solution 2 - C#

Assuming you know the real directory the XML file lives in use Path.Combine, e.g.

var absolute_path = Path.Combine(directoryXmlLivesIn, "..\images\image.jpg");

If you want to get back the full path with any ..'s collapsed then you can use:

Path.GetFullPath((new Uri(absolute_path)).LocalPath);

Solution 3 - C#

This worked.

var s = Path.Combine(@"C:\some\location", @"..\other\file.txt");
s = Path.GetFullPath(s);

Solution 4 - C#

It`s best way for convert the Relative path to the absolute path!

string absolutePath = System.IO.Path.GetFullPath(relativePath);

Solution 5 - C#

You can use Path.Combine with the "base" path, then GetFullPath on the results.

string absPathContainingHrefs = GetAbsolutePath(); // Get the "base" path
string fullPath = Path.Combine(absPathContainingHrefs, @"..\..\images\image.jpg");
fullPath = Path.GetFullPath(fullPath);  // Will turn the above into a proper abs path

Solution 6 - C#

Have you tried Server.MapPath method. Here is an example

string relative_path = "/Content/img/Upload/Reports/59/44A0446_59-1.jpg";
string absolute_path = Server.MapPath(relative_path);
//will be c:\users\.....\Content\img\Upload\Reports\59\44A0446_59-1.jpg

Solution 7 - C#

This worked for me.

//used in an ASP.NET MVC app
private const string BatchFilePath = "/MyBatchFileDirectory/Mybatchfiles.bat"; 
var batchFile = HttpContext.Current.Server.MapPath(BatchFilePath);

Solution 8 - C#

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndersView Question on Stackoverflow
Solution 1 - C#cahit beyazView Answer on Stackoverflow
Solution 2 - C#PaoloView Answer on Stackoverflow
Solution 3 - C#David CrowellView Answer on Stackoverflow
Solution 4 - C#Mohammad Fathi MiMFaView Answer on Stackoverflow
Solution 5 - C#Reed CopseyView Answer on Stackoverflow
Solution 6 - C#Tianzhen LinView Answer on Stackoverflow
Solution 7 - C#Jonny BoyView Answer on Stackoverflow
Solution 8 - C#JeremyWeirView Answer on Stackoverflow