想開啟 Windows 目錄底下的某一個檔案, 使用以下的敘述對嗎?
Open "C:\Windows\" & 檔案名稱 For Input As #1
通常是對的, 如果使用很規則地把 Windows 安裝在 C:\Windows 目錄底下, 但不對的風險很大, 因此我們需要一個讀取 Windows 所在目錄的方法, 此時需使用 GetWindowsDirectory 及API 函數, 以下則是使用的細節:
1. API 的宣告:
Private Declare Function GetWindowsDirectory Lib "kernel32"
Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal
nSize As Long) As Long
Private Declare Function GetSystemDirectory Lib "kernel32" Alias
"GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize
As Long) As Long
註:如果以上的宣告放在「一般模組」底下, 應將 Private 保留字去掉。
2. 呼叫範例:
Dim S As String * 80, Length As Long
Dim WinPath As String, SysPath As String
Length = GetWindowsDirectory(S, Len(S))
WinPath = Left(S, Length)
Length = GetSystemDirectory(S, Len(S))
SysPath = Left(S, Length)
結果:WinPath 等於 Windows 的所在目錄, SysPath 等於 Windows 的 System 所在目錄。