Map a relative path to absolute?

Hello, I'm familiar with web applications and Server.MapPath in asp.net.  I'm looking for something similar to use in a console applicaton.  It would allow me to transform a relative path e.g. "..\..\somepath\",  to an absolute path "c:\my apps\deploy\somepath".  It would start the process in the directory the exe is currently located in.

for example if the exe is located in
"c:\my apps\app1\myapp.exe"

and I pass this argument "..\app2\"
I'd end up with
"c:\my apps\app2\"

LVL 33
raterusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Luis PérezSoftware Architect in .NetCommented:
It's easy using System.IO.DirectoryInfo object. Use the snippet function attached. Hope that helps.
    Public Function GetAbsolutePath(ByVal StartPath As String, ByVal RelativePath As String) As String
        'Example usage:
        'Dim sPath As String = GetAbsolutePath("C:\My Documents\My Images\Photos\My Photos\Captured", "\..\..\..\..\My Videos") - this will return "C:\My Documents\My Videos" - note that works only based on real directories on hard disk, not simply parsing strings
 
        'Start with the Startup path
        Dim sCompletePath As String = StartPath
        'Add a trailing backslash
        If (Not sCompletePath.EndsWith("\")) Then sCompletePath += "\"
        'Remove a beggining backslash from relative path
        If RelativePath.StartsWith("\") Then RelativePath = RelativePath.Substring(1)
        'Concat startup path and relative path
        sCompletePath += RelativePath
 
        'Get a DirectoryInfo object of the complete path (including relative part)
        Dim oNewPath As New System.IO.DirectoryInfo(sCompletePath)
 
        'Return full name (DirectoryInfo has resolved relative part of the path)
        Return oNewPath.FullName
    End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
raterusAuthor Commented:
It's working great, thanks!

I also found that this did roughly the same thing, but you're stuck using the current directory of the application.
System.IO.Path.GetFullPath("..\somepath\")
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.