Главная страницаОбратная связьКарта сайта

Найти 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
sgBox "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


Обсудить статью на форуме


Если Вас заинтересовала или понравилась информация программирование на Visual Basic - "Найти TEMP-директорию и создать новый .tmp-файл", Вы можете поставить закладку в социальной сети или в своём блоге на данную страницу:

Так же Вы можете задать вопрос по работе этого модуля или примера через форму обратной связи, в сообщение обязательно указывайте название или ссылку на статью!
   


Copyright © 2008 - 2024 Дискета.info