Map a relative path to absolute?

Posted on 2008-11-10
Last Modified: 2012-05-05
Hello, I'm familiar with web applications and Server.MapPath in  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\"

Question by:raterus
    LVL 25

    Accepted Solution

    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

    LVL 33

    Author Closing Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    This video discusses moving either the default database or any database to a new volume.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now