Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Jul 24, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Windows API

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:\Windows\Temp. 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 by the system. This lets you keep your own temporary files confined to a known location. In addition, you don't have to guess where you can put them 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
   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 your application:
Debug.Print TempDirectory
That will probably print something like this line:
Either way, it's the temporary directory in use by Windows, so you can be sure it exists.
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date