Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ..\?

like image 607
Mehdi Anis Avatar asked Apr 07 '10 02:04

Mehdi Anis


People also ask

How do you convert relative path to absolute path?

The absolutePath function works by beginning at the starting folder and moving up one level for each "../" in the relative path. Then it concatenates the changed starting folder with the relative path to produce the equivalent absolute path.

What is absolute and relative path explain with example?

For example, /home/sally/statusReport is an absolute path. All of the information needed to locate the file is contained in the path string. A relative path needs to be combined with another path in order to access a file. For example, joe/foo is a relative path.

What is an absolute path What is a relative path?

A relative path describes the location of a file relative to the current (working) directory*. An absolute path describes the location from the root directory. When learning to access data files through programming, we regularly use relative file paths.

How do I get the relative path of a file in Visual Studio code?

Relative Path Extension for VS CodePress Ctrl+Shift+H (Mac: Cmd+Shift+H ) and start typing the file you want.


1 Answers

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

like image 116
shf301 Avatar answered Oct 12 '22 12:10

shf301