MSDN Documentation

GetTempPath function

Retrieves the path of the directory designated for temporary files.

Syntax

DWORD GetTempPathA(
    DWORD   nBufferLength,
    LPSTR   lpBuffer
);

DWORD GetTempPathW(
    DWORD   nBufferLength,
    LPWSTR  lpBuffer
);

Parameters

  • nBufferLength – The size of the buffer pointed to by lpBuffer, in TCHARs.
  • lpBuffer – A pointer to a buffer that receives the null-terminated string containing the temporary file path.

Return value

If the function succeeds, the return value is the length, in TCHARs, of the string copied to lpBuffer, not including the terminating null character. If the return value is greater than nBufferLength, the buffer was too small and the string was truncated. If the function fails, the return value is zero. Call GetLastError for extended error information.

Remarks

The temporary path is determined by the following precedence:

  1. The path specified by the TMP environment variable.
  2. The path specified by the TEMP environment variable.
  3. The path of the Windows directory.
  4. The root directory of the system drive.

The returned path includes a trailing backslash.

Example

#include <windows.h>
#include <stdio.h>

int main(void)
{
    TCHAR path[MAX_PATH];
    DWORD len = GetTempPath(MAX_PATH, path);
    if (len == 0 || len > MAX_PATH) {
        printf("Failed to get temp path. Error: %lu\n", GetLastError());
        return 1;
    }
    _tprintf(_T("Temp path: %s\n"), path);
    return 0;
}