Mehdi Anis
Mehdi Anis

Reputation: 358

Relative path to absolute path in VB.NET

I am writing a VB.NET console application where it takes relative paths and spits out all file names, or an error for invalid input. I am having trouble getting PhysicalPath from relative path

Example:

  1. I am in folder C:\Documents and Settings\MehdiAnis.ULTIMATEBANGLA\My Documents\Visual Studio 2005\Projects\SP_Sol\SP_Proj\bin\Debug

  2. My application, SP.exe, is also in the same folder.

  3. I run: "SP.exe ..\". The output will be a list of all files in the folder "C:\Documents and Settings\MehdiAnis.ULTIMATEBANGLA\My Documents\Visual Studio 2005\Projects\SP_Sol\SP_Proj\bin"

  4. I run: "SP.exe ..\\..\". The output will be a list of all files in the folder "C:\Documents and Settings\MehdiAnis.ULTIMATEBANGLA\My Documents\Visual Studio 2005\Projects\SP_Sol\SP_Proj"

  5. I run: "SP.exe ..\\..\\..\". The output will be a list of all files in the folder "C:\Documents and Settings\MehdiAnis.ULTIMATEBANGLA\My Documents\Visual Studio 2005\Projects\SP_Sol"

Currently I am handling one relative path, but no more:

    If Source.IndexOf("..\") = 0 Then
        Dim Sibling As String = Directory.GetParent(Directory.GetCurrentDirectory()).ToString()())
        Source = Source.Replace("..\", Sibling)
    End If

How can I easily handle multiple ..\?

Upvotes: 14

Views: 32043

Answers (1)

shf301
shf301

Reputation: 31394

You're looking for System.IO.Path.GetFullPath(). It should handle any type of relative path.

Upvotes: 26

Related Questions