Home Articles Books Downloads FAQs Tips

Q: Get the location of the Windows temp directory

Answer: Windows contains a temp directory that you can use to store temporary files. The temp directory is often C:\WINDOWS\TEMP, but sometimes it's just C:\TEMP. The API provides a function called GetTempPath that allows you to determine the path for the temp directory.

char TempDir[MAX_PATH +1];
TempDir[MAX_PATH] = '\0';
GetTempPath(MAX_PATH,TempDir);

Note: The programmer who coded the GetTempPath function evidently works in a different department at Microsoft than the person who coded the GetWindowsDirectory function. Notice that GetTempPath wants the size argument first, whereas GetWindowsDirectory wants the pointer to the string passed first.

To further support my theory that these two developers work on opposite ends of Microsoft campus, consider the fact that the string returned by GetTempPath contains a trailing slash. The string returned by GetWindowsDirectory does not contain a trailing slash. Keep this in mind when you need to concatenate a file name onto the directory name.

Note: The API also provides a function called GetTempFileName that allows you to create unique temporary file names. It is your responsibility to delete the file when you are done using it.



Copyright © 1997-2000 by Harold Howe.
All rights reserved.