Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative path to absolute path in C#?

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?

like image 226
Anders Avatar asked Jan 25 '11 16:01

Anders


2 Answers

string exactPath = Path.GetFullPath(yourRelativePath); 

works

like image 126
cahit beyaz Avatar answered Oct 17 '22 08:10

cahit beyaz


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); 
like image 39
Paolo Avatar answered Oct 17 '22 06:10

Paolo