Jan 082011

This is a really simple function, but I use it all the time. It creates a path to a temporary file name that can be used, well, temporarily. The files are not cleaned up or anything, but they are not going to conflict with other files that may be in the same temp folders.

Enjoy.

CString GetTempFilePath(LPCTSTR strPattern)
{
    CString csPath;
    if(GetTempPath(_MAX_PATH,csPath.GetBuffer(_MAX_PATH+1)) != 0) {
        csPath.ReleaseBuffer();
        CString csTempFile;
        if(GetTempFileName(csPath,strPattern,0,csTempFile.GetBuffer(_MAX_PATH+1)) != 0) {
            csTempFile.ReleaseBuffer();
            return csTempFile;
        }
    }
    return CString();
}

Leave a Reply

(required)

(required)