Tip 134: Creating Temporary Files

July 1, 1995

Abstract

When developing an application in Microsoft® Visual Basic®, you may need to create a temporary file on disk. This article explains how to create temporary files in Visual Basic version 4.0.

Using the GetTempFileName Function

You can create a new file on a specified disk drive using the Microsoft® Windows® application programming interface (API) GetTempFileName function. Although the file you create is referred to as a temporary file, you are responsible for physically deleting the file from disk when you no longer need it.

To use the GetTempFileName function in Microsoft® Visual Basic®, you must include the following Declare statement in your program (note that this Declare statement must be typed as a single line of code):

Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" 
   (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As 
   Long, ByVal lpTempFileName As String) As Long

The GetTempFileName function requires four arguments. The first argument must be set to the drive and/or path where you want to create the new file. In the example program shown below, the new file is created in the root directory on drive C.

The second argument required by the GetTempFileName function is the prefix you want to assign to the file name. In other words, if you specify the prefix as being "TEST", the function will create the first four letters of the new file name set to "TEST".

The third argument to the GetTempFileName function should be set to zero. This tells the function to automatically generate a random number for the file name. This random number is then appended to the prefix string to create a unique and complete file name.

The fourth argument to the GetTempFileName function requires a string buffer of at least 256 characters in length to hold the temporary file's name.

After you call this function, the new file is created on the specified disk. It is important to note that the file is not deleted when you quit your application—you must physically delete the file from disk.

Example Program

This program shows how to create a temporary file from within your Visual Basic application.

  1. Create a new project in Visual Basic. Form1 is created by default.

  2. Add the following Declare statement to the General Declarations section of Form1 (note that this Declare statement must be typed as a single line of code):
    Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" 
       (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As 
       Long, ByVal lpTempFileName As String) As Long
    
  3. Add the following code to the Form_Load event for Form1:
    Private Sub Form_Load()
        Text1.TEXT = ""
    End Sub
    
  4. Add a Text Box control to Form1. Text1 is created by default.

  5. Add a Command Button control to Form1. Command1 is created by default.

  6. Add the following code to the Click event for Command1:
    Private Sub Command1_Click()
        Dim FilePrefix As String
        Dim NewFile As String * 256
        FilePrefix = "TEST"
        NewFile = GetTempName(FilePrefix)
        Text1.TEXT = NewFile
    End Sub
    
  7. Create a new function called GetTempName. Add the following code to this function:
    Private Function GetTempName(TmpFilePrefix As String) As String
        Dim TempFileName As String * 256
        Dim X As Long
        Dim DriveName As String
        DriveName = "c:\"
        X = GetTempFileName(DriveName, TmpFilePrefix, 0, TempFileName)
        GetTempName = Left$(TempFileName, InStr(TempFileName, Chr(0)) - 1)
    End Function
    

Run the example program by pressing F5. Click the command button to create a new temporary file on drive C in the root directory. The name of the newly created file is displayed in the Text Box control.