找出正確的目前檔案路徑-GetAppPathName

我們可以用App.Path找出自己的檔案路徑,然後在同一目錄中找一些檔案,假設目前程式的位置在

C:\Windows\ABC.EXE

我想在同一目錄找Gold.BMP,於是輸入App.Path & "\Gold.BMP"就是該檔案的位置了!可以當我的程式放在根目裡(如C:\),那裡App.Path & "\Gold.BMP"就不是該檔案的路徑!因此得出的路徑C:\\Gold.BMP不成立!

為了使程式在一般位置或是根目錄都可以正確找出當前目錄的檔案,站長提供了以下的一個函數:

Private Function GetAppPathName() As String
Dim ST As String
 ST = App.Path
 If Right(ST, 1) = "\" Then ST = Left(ST, 2)
 GetAppPathName = ST
End Function

用法:

Private Sub Command1_Click()
 MsgBox GetAppPathName & "\Gold.bmp" '得出的路徑在根目錄或是一般目錄都可以正常讀取
End Sub

Private Function GetAppPathName() As String
Dim ST As String
  ST = App.Path
 If Right(ST, 1) = "\" Then ST = Left(ST, 2)
  GetAppPathName = ST
End Function


上一頁