Get the Temp Directory

By | 2019-09-30

Uses Kernel32 to retrieve the path for the Windows temporary directory.

Module

Private Declare Function GetTempPath Lib "kernel32.dll" Alias "GetTempPathA" ( _
    ByVal nBufferLength As Long, _
    ByVal lpBuffer As String _
    ) As Long
Public Function GetTempDir() As String
' Returns the temp directory from the OS system
    Dim sBuffer As String
    Dim HoldBuffer As String
    Dim iBuffLen As Long
    Dim iReturn As Long
    Const BUFFER_LENGTH = 256

    iBuffLen = BUFFER_LENGTH
    sBuffer = Space(BUFFER_LENGTH)
    iReturn = GetTempPath(iBuffLen, sBuffer) '* get rid of the null character
    HoldBuffer = Left$(sBuffer, iBuffLen - 1)
    GetTempDir = Left$(HoldBuffer, InStr(HoldBuffer, vbNullChar) - 1)
End Function

Usage

Private Sub Command1_Click()
    MsgBox GetTempDir
End Sub

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.