明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

如何取得系統(tǒng)的臨時目錄路徑?

[摘要]GetTempPath取得的是一個DOS名稱,當文件名長度大于8時,長文件名格式“C:\Documents and Settings\Administrator\Local Settings\Temp”會顯示成“C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp”的短文件名格式,...
GetTempPath取得的是一個DOS名稱,當文件名長度大于8時,長文件名格式“C:\Documents and Settings\Administrator\Local Settings\Temp”會顯示成“C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp”的短文件名格式,如何根據自己需要取得系統(tǒng)臨時目錄的途徑,下面是一個新寫的函數,調用兩個API 實現,相信許多朋友可能用得到。

Option Explicit
Private Declare Function GetLongPathName Lib "kernel32" Alias "GetLongPathNameA" (ByVal lpszShortPath As String, ByVal lpszLongPath As String, ByVal cchBuffer As Long) As Long
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long


Sub GetTEMPfolder(Optional ByVal showlong As Boolean = True)

Dim longname As String, shortname As String

shortname = Space(256)
GetTempPath Len(shortname), shortname

longname = Space(1024)
GetLongPathName shortname, longname, Len(longname)



MsgBox "Tempfolder : " & IIf(showlong = True, longname, shortname)

End Sub

Private Sub Command1_Click()
GetTEMPfolder '長文件名
GetTEMPfolder False '短文件名
End Sub