|
Найти TEMP-директорию и создать новый .tmp-файл |
|
|
Как найти временную папку Windows используя (1)
функцию ENVIRON или (2) используя Win32 API GetTempPath. Эта
программа покажет, как сгенерировать новое
временное имя, используя GetTempFileName. Добавьте на
форму CommandButton и Label Private Declare Function GetTempPath Lib "kernel32"
Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As
Long
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 Command1_Click()
Dim ls_TempPath As String
Dim ll_Buffer As Long
Dim ls_TempFileName As String
ll_Buffer = 255
ls_TempPath = Space(255)
If GetTempPath(ll_Buffer, ls_TempPath) = 0 Then
MsgBox "API Failed!"
Else
ls_TempPath = Left(ls_TempPath, ll_Buffer)
End If
ls_TempFileName = Space(255)
ll_Buffer = GetTempFileName(ls_TempPath, "xxx", 0, ls_TempFileName)
'xxx is a three letter prefix - can be anything you want.
'3rd parameter (0 above) is uUnique...If uUnique is nonzero, the function appends the
hexadecimal string to lpPrefixString to form the temporary filename. In this case, the
function does not create the specified file, and does not test whether the filename is
unique.
'If uUnique is zero, the function uses a hexadecimal string derived from the current
system time. In this case, the function uses different values until it finds a unique
filename, and then it creates the file in the lpPathName directory.
If ll_Buffer = 0 Then
MsgBox "API Failed!"
Else
ls_TempFileName = Left(ls_TempFileName, ll_Buffer)
MsgBox "Created temporary file :" & ls_TempFileName
End If
End Sub
Private Sub Form_Load()
Dim ls_TempPath As String
Dim ll_Buffer As Long
Dim li_Length As Integer
ll_Buffer = 255
ls_TempPath = Space(255)
li_Length = GetTempPath(ll_Buffer, ls_TempPath)
If li_Length = 0 Then
MsgBox "API Failed!"
Else
ls_TempPath = Left(ls_TempPath, li_Length)
Label1 = "Temporary Directory is " & ls_TempPath
End If
'Or Using environment variables.
'To see your environment variables go to the system icon in control panel and click on the
environment tab. You can get the value of these variables from VB using the command
ENVIRON. e.g.
'Label1 = Environ("TEMP")
'Label1 = Environ("TMP")
End Sub
|
|
|
|
|
|
|