Visual Basic Programming Code Examples Visual Basic > Windows and Controls Code Examples Return the path of the temporary directory Return the path of the temporary directory The following routine returns the path of the temporary directory/folder: Option Explicit Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long 'Purpose : Returns the path of the temporary directory 'Inputs : N/A 'Outputs : The temporary path name 'Notes : Can use Environ("Temp") as an alternative to the API call on NT machines Function DirTemp() As String Dim lLenTemp As Long Static ssTempDir As String If Len(ssTempDir) = 0 Then lLenTemp = 150 ssTempDir = String$(lLenTemp, Chr$(0)) 'Get the username lLenTemp = GetTempPath(lLenTemp, ssTempDir) 'strip the rest of the buffer ssTempDir = Left$(ssTempDir, lLenTemp) If Right$(ssTempDir, 1) <> "\" Then ssTempDir = ssTempDir & "\" End If End If DirTemp= ssTempDir End Function