Detect Window Temp folder with VC++, VC++ example


The GetTempPath function retrieves the path of the directory designated for temporary files.

DWORD GetTempPath(
DWORD nBufferLength,
LPTSTR lpBuffer


[in] Size of the string buffer identified by lpBuffer, in TCHARs.
[out] Pointer to a string buffer that receives the null-terminated string specifying the temporary file path. The returned string ends with a backslash, for example, C:\TEMP\.

Return Values

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 return value is the length, in TCHARs, of the buffer required to hold the path.

If the function fails, the return value is zero. To get extended error information, call GetLastError.


The GetTempPath function checks for the existence of environment variables in the following order and uses the first path found:

  1. The path specified by the TMP environment variable.
  2. The path specified by the TEMP environment variable.
  3. The path specified by the USERPROFILE environment variable.
  4. The Windows directory.

Note that the function does not verify that the path exists.

Windows Me/98/95: If TMP and TEMP are not set to a valid path, GetTempPath uses the current directory.

Applications can obtain unique file names for temporary files using the GetTempFileName function. The GetTempPath function retrieves the path to the directory where temporary files should be created.

The following procedure shows you how an application copies one file to another.

To copy one file to another

  1. The application opens the Original.txt file by using CreateFile.
  2. The application retrieves a temporary file path and file name by using the GetTempPath and GetTempFileName functions, and then uses CreateFile to create the temporary file.
  3. The application reads 64K blocks into a buffer, converts the buffer contents to uppercase, and writes the converted buffer to the temporary file.
  4. When all of Original.txt is written to the temporary file, the application closes both files, and renames the temporary file to Allcaps.txt by using the MoveFileEx function.

The following example shows you the code to copy one file to another, and notes that the target file is an uppercase version of the first file.


#define BUFSIZE 4096

int main()
HANDLE hTempFile;
DWORD dwBytesRead, dwBytesWritten, dwBufSize=BUFSIZE;
char szTempName[MAX_PATH];
char buffer[BUFSIZE];
char lpPathBuffer[BUFSIZE];

// Open the existing file.

hFile = CreateFile("original.txt", // file name
GENERIC_READ, // open for reading
0, // do not share
NULL, // default security
OPEN_EXISTING, // existing file only
NULL); // no template
printf("Could not open file.");
return 0;

// Get the temp path

GetTempPath(dwBufSize, // length of the buffer
lpPathBuffer); // buffer for path

// Create a temporary file.

GetTempFileName(lpPathBuffer, // directory for temp files
"NEW", // temp file name prefix
0, // create unique name
szTempName); // buffer for name

hTempFile = CreateFile((LPTSTR) szTempName, // file name
GENERIC_READ | GENERIC_WRITE, // open for read/write
0, // do not share
NULL, // default security
CREATE_ALWAYS, // overwrite existing file
NULL); // no template

printf("Could not create temporary file.");
return 0;

// Read 4K blocks to the buffer.
// Change all characters in the buffer to upper case.
// Write the buffer to the temporary file.

if (ReadFile(hFile, buffer, 4096,
&dwBytesRead, NULL))
CharUpperBuff(buffer, dwBytesRead);

WriteFile(hTempFile, buffer, dwBytesRead,
&dwBytesWritten, NULL);
} while (dwBytesRead == BUFSIZE);

// Close both files.


// Move the temporary file to the new text file.

if (!MoveFileEx(szTempName,
printf("Could not move temp file.");
return 0;

0 nhận xét:


Coding experience share Copyright © 2010 | Designed by Ipietoon for Free Blogger Template