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:
I am in folder C:\Documents and Settings\MehdiAnis.ULTIMATEBANGLA\My Documents\Visual Studio 2005\Projects\SP_Sol\SP_Proj\bin\Debug
My application, SP.exe
, is also in the same folder.
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"
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"
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 ..\
?
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.
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.
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.
Relative Path Extension for VS CodePress Ctrl+Shift+H (Mac: Cmd+Shift+H ) and start typing the file you want.
You're looking for System.IO.Path.GetFullPath(). It should handle any type of relative path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With