Dot-Net

VB.NET中絕對路徑的相對路徑

  • September 7, 2019

我正在編寫一個 VB.NET 控制台應用程序,它採用相對路徑並吐出所有文件名,或者無效輸入的錯誤。我無法從相對路徑獲取 PhysicalPath

例子:

  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"

目前我正在處理一個相對路徑,但沒有更多:

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

我怎樣才能輕鬆處理多個..\

您正在尋找System.IO.Path.GetFullPath()。它應該處理任何類型的相對路徑。

引用自:https://stackoverflow.com/questions/2589590