Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get absolute file path from base path and relative containing ".."?

Tags:

c#

.net

filepath

string basepath = @"C:\somefolder\subfolder\bin"; // is defined in runtime
string relative = @"..\..\templates";

string absolute = Magic(basepath, relative); // should be "C:\somefolder\templates"

Can you help me with Magic method? Hopefully not too complicated code.

Is there the "Magic" method in .NET Framework?

like image 746
Serge S. Avatar asked Jun 29 '10 09:06

Serge S.


1 Answers

If you look at the Path class there are a couple of methods which should help:

Path.Combine

and

Path.GetFullPath

So:

string newPath = Path.Combine(basepath, relative);
string absolute = Path.GetFullPath(newPath);

Although the second step isn't strictly needed - it would give you a "cleaner" path if you were printing out say.

like image 79
ChrisF Avatar answered Sep 28 '22 22:09

ChrisF