devxlogo

Windows API

Windows API

Question:
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.

Answer:
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 Function
Next, 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 Long
That’s all there is to it. Try this line of code somewhere in yourapplication:
Debug.Print TempDirectory
That will probably print something like this line:
C:TMP
Either way, it’s the temporary directory in use by Windows, so you can besure it exists.
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist