I’m writing a program which needs to clean up the temp files which are in the Temp directory as define by the Temp Environment setting. I don’t want the program to assume that the Temp directory is always C:WindowsTemp. There for I was wondering if there is an API or some other method to determine where is the Temp directory on the users PC. The program is being written in VB 3 Pro and running under Windows95, and all people using this program are running Windows95 as well.
Here’s an answer from my new book about this exact API call:
The GetTempPath API call returns the current temporary directory in use bythe system. This lets you keep your own temporary files confined to aknown location. In addition, you don’t have to guess where you can putthem or have to resort to putting them in the home directory.
The code for the TempDirectory function is listed here:
Public Function TempDirectory() As String Dim sBuffer As String Dim iBufLen As Long Dim iReturn As Long iBufLen = BUFFER_LENGTH sBuffer = Space(BUFFER_LENGTH) iReturn = GetTempPath(iBufLen, sBuffer) TempDirectory = Left$(sBuffer, iBufLen – 1)End FunctionNext, add this API declaration to the declarations section of the module:
Declare Function GetTempPath _ Lib “kernel32” _ Alias “GetTempPathA” _ (ByVal nBufferLength As Long, _ ByVal lpBuffer As String) As LongThat’s all there is to it. Try this line of code somewhere in yourapplication:
Debug.Print TempDirectoryThat will probably print something like this line:
C:TMPEither way, it’s the temporary directory in use by Windows, so you can besure it exists.