Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Map a relative path to absolute?

Posted on 2008-11-10
Medium Priority
Last Modified: 2012-05-05
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\"

Question by:raterus
LVL 25

Accepted Solution

Luis Pérez earned 2000 total points
ID: 22923147
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

ID: 31515032
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.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

564 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