'获取TEMP临时文件路径的API函数 Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Private Sub command1_click() Me.Caption = "获取TEMP路径" Me.Cls '也可以用 MsgBox Environ("temp") 来实现 Label1.Caption = GetTemp() End Sub http://hi.baidu.com/icecept
'获取Temp路径函数 Function GetTemp() As String Dim Temp As String * 80, Length As Long Length = GetTempPath(Len(Temp), Temp) GetTemp = Left(Temp, Length) '返回:盘符:\temp\ If Right(GetTemp, 1) = "\" Then ' GetTemp = Left(GetTemp, Length - 1) End If End Function
Private Declare Function SHGetPathFromIDList _ Lib "shell32.dll" Alias "SHGetPathFromIDListA" _ (ByVal pidl As Long, _ ByVal pszPath As String) As LongPrivate Declare Function SHGetSpecialFolderLocation _ Lib "shell32.dll" _ (ByVal hwndOwner As Long, _ ByVal nFolder As Long, _ pidl As Long) As Long Private Function GetSpecialPath(hwnd, CSIDL As Long) As String Dim r As Long Dim path As String Dim pidl As Long r = SHGetSpecialFolderLocation(hwnd, CSIDL, pidl) If r = NOERROR Then path = Space$(512) r = SHGetPathFromIDList(ByVal pidl, ByVal path) GetSpecialPath = Left$(path, InStr(path, Chr$(0)) - 1) Exit Function End If GetSpecialPath = "" End FunctionPrivate Sub Form_Load() MsgBox GetSpecialPath(Me.hwnd, CSIDL_INTERNET_CACHE) End Sub
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Private Sub command1_click()
Me.Caption = "获取TEMP路径"
Me.Cls
'也可以用 MsgBox Environ("temp") 来实现
Label1.Caption = GetTemp()
End Sub
http://hi.baidu.com/icecept
Function GetTemp() As String
Dim Temp As String * 80, Length As Long
Length = GetTempPath(Len(Temp), Temp)
GetTemp = Left(Temp, Length) '返回:盘符:\temp\
If Right(GetTemp, 1) = "\" Then '
GetTemp = Left(GetTemp, Length - 1)
End If
End Function
您还可以使用FileSystemObject对象的GetTempName()方法来获得临时文件名.
Lib "shell32.dll" Alias "SHGetPathFromIDListA" _
(ByVal pidl As Long, _
ByVal pszPath As String) As LongPrivate Declare Function SHGetSpecialFolderLocation _
Lib "shell32.dll" _
(ByVal hwndOwner As Long, _
ByVal nFolder As Long, _
pidl As Long) As Long
Private Function GetSpecialPath(hwnd, CSIDL As Long) As String
Dim r As Long
Dim path As String
Dim pidl As Long
r = SHGetSpecialFolderLocation(hwnd, CSIDL, pidl)
If r = NOERROR Then
path = Space$(512)
r = SHGetPathFromIDList(ByVal pidl, ByVal path)
GetSpecialPath = Left$(path, InStr(path, Chr$(0)) - 1)
Exit Function
End If
GetSpecialPath = ""
End FunctionPrivate Sub Form_Load()
MsgBox GetSpecialPath(Me.hwnd, CSIDL_INTERNET_CACHE)
End Sub