Relative path to absolute path in C#?
Asked Answered
M

8

116

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?

Moia answered 25/1, 2011 at 16:40 Comment(2)
Is this a desktop or web applicaiton?Autograph
Possible Duplicate how to convert relative path to absolute path in windows application?.Annihilator
S
119

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);
Smallsword answered 25/1, 2011 at 16:46 Comment(4)
Thanks, my problem was apparently that I had forgotten to get the directory of the xml file first, I had tried Combine, but with the absolute path of the file, which didn't work. I don't know which of all these answers was first, the time shows the same, but you pointed out to get the directory, so I chose your answer. Thanks to all though!Moia
With a little bit of experimentation it appears that (new Uri(absolute_path)).LocalPath does the same thing as Path.GetFullPath(absolute_path) so one or the other should suffice.Linette
Note that Uri has special handling of "#" or "&" which you don't want when handling paths and filenames!Masonmasonic
@MatsW: learn.microsoft.com/en-us/dotnet/api/system.uri.localpath states Gets a local operating-system representation of a file name. so in this case, no special handling is to be expected.Taker
C
179
string exactPath = Path.GetFullPath(yourRelativePath);

works

Carisa answered 25/9, 2014 at 8:45 Comment(2)
this the right answer to the question 'how do you convert a relative path to an absolute path?' the OP just wanted to know the answer to 'how do you stick to bits of paths together?'Stokowski
At first I thought this didn't work, but it does. If you feed your relative path of C:\test\A\..\B\test.txt The output will be C:\test\B\test.txtPlanter
S
119

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);
Smallsword answered 25/1, 2011 at 16:46 Comment(4)
Thanks, my problem was apparently that I had forgotten to get the directory of the xml file first, I had tried Combine, but with the absolute path of the file, which didn't work. I don't know which of all these answers was first, the time shows the same, but you pointed out to get the directory, so I chose your answer. Thanks to all though!Moia
With a little bit of experimentation it appears that (new Uri(absolute_path)).LocalPath does the same thing as Path.GetFullPath(absolute_path) so one or the other should suffice.Linette
Note that Uri has special handling of "#" or "&" which you don't want when handling paths and filenames!Masonmasonic
@MatsW: learn.microsoft.com/en-us/dotnet/api/system.uri.localpath states Gets a local operating-system representation of a file name. so in this case, no special handling is to be expected.Taker
Z
36

This worked.

var s = Path.Combine(@"C:\some\location", @"..\other\file.txt");
s = Path.GetFullPath(s);
Zarf answered 25/1, 2011 at 16:48 Comment(0)
A
15

It's the best way to convert the Relative path to the Absolute one!

string absolutePath = System.IO.Path.GetFullPath(relativePath);
Archi answered 2/12, 2018 at 7:44 Comment(0)
J
8

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
Jardiniere answered 25/1, 2011 at 16:43 Comment(0)
C
6

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
Caithness answered 25/1, 2011 at 16:43 Comment(0)
S
1

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);
Scrap answered 11/4, 2018 at 13:15 Comment(0)
C
0

Take a look at Path.Combine http://msdn.microsoft.com/en-us/library/fyy7a5kt.aspx

Collop answered 25/1, 2011 at 16:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.