VB.NET中绝对路径的相对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2589590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:08:40  来源:igfitidea点击:

Relative path to absolute path in VB.NET

.netpathrelative-pathabsolute-path

提问by Mehdi Anis

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

我正在编写一个 VB.NET 控制台应用程序,它采用相对路径并吐出所有文件名,或者输入无效的错误。我无法从相对路径获取 PhysicalPath

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"

  1. 我在文件夹中 C:\Documents and Settings\MehdiAnis.ULTIMATEBANGLA\My Documents\Visual Studio 2005\Projects\SP_Sol\SP_Proj\bin\Debug

  2. 我的应用程序SP.exe也位于同一文件夹中。

  3. 我跑:"SP.exe ..\"。输出将是文件夹中所有文件的列表"C:\Documents and Settings\MehdiAnis.ULTIMATEBANGLA\My Documents\Visual Studio 2005\Projects\SP_Sol\SP_Proj\bin"

  4. 我跑:"SP.exe ..\\..\"。输出将是文件夹中所有文件的列表"C:\Documents and Settings\MehdiAnis.ULTIMATEBANGLA\My Documents\Visual Studio 2005\Projects\SP_Sol\SP_Proj"

  5. 我跑:"SP.exe ..\\..\\..\"。输出将是文件夹中所有文件的列表"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 ..\?

如何轻松处理多个..\

回答by shf301

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

您正在寻找System.IO.Path.GetFullPath()。它应该处理任何类型的相对路径。