July 1, 1995
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.
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.
This program shows how to create a temporary file from within your Visual Basic application.
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
Private Sub Form_Load()
Text1.TEXT = ""
End Sub
Private Sub Command1_Click()
Dim FilePrefix As String
Dim NewFile As String * 256
FilePrefix = "TEST"
NewFile = GetTempName(FilePrefix)
Text1.TEXT = NewFile
End Sub
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.