mtempfile.bas

来自「一个关于电脑管理汽车的软件」· BAS 代码 · 共 68 行

BAS
68
字号
Attribute VB_Name = "mTempFile"
Option Explicit
Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

' Data structure needed for Windows API call (GetSystemTime)
Public Type SYSTEMTIME
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
End Type

' Windows API call to get system time from os
Public Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)

Public Function Get_Temp_Path() As String
Dim TempPath As String  ' receives name of temporary file path
Dim TempFile As String  ' receives name of temporary file
Dim slength As Long  ' receives length of string returned for the path
Dim lastfour As Long  ' receives hex value of the randomly assigned ????
Dim tmpFileName As String
    ' Get Windows's temporary file path
    TempPath = Space(255)  ' initialize the buffer to receive the path
    slength = GetTempPath(255, TempPath)  ' read the path name
    TempPath = Left(TempPath, slength)  ' extract data from the variable
    Get_Temp_Path = TempPath
End Function
Public Function FormatPath(sPath As String) As String
    sPath = Trim$(sPath)
    If sPath = "" Then
        FormatPath = ""
        Exit Function
    End If
    FormatPath = IIf(Right$(sPath, 1) <> "\", sPath & "\", sPath)
End Function

Public Function Get_Temp_File(Optional PrependString As String = "", Optional Extension As String = ".tmp") As String
Dim lpSystemTime As SYSTEMTIME
Dim theTemp As String
Dim RandomNumber As Integer
    
    'Get random number between 1 and 999 - will be appended to the result to ensure uniqueness
    Randomize
    RandomNumber = Int((999 * Rnd) + 1)
    
    GetSystemTime lpSystemTime
    theTemp = PrependString & Trim$(lpSystemTime.wYear) & Format(Trim$(lpSystemTime.wMonth), "00") & Format(Trim$(lpSystemTime.wDay), "00") & Format(Trim$(lpSystemTime.wHour), "00") & Format(Trim$(lpSystemTime.wMinute), "00") & Format(Trim$(lpSystemTime.wSecond), "00") & Format(Trim$(lpSystemTime.wMilliseconds), "000") & Format(Trim$(CStr(RandomNumber)), "000") & Extension
    Get_Temp_File = theTemp

End Function
Public Function SafeKill(theFile As String) As String
    SafeKill = ""
    On Error Resume Next
    SetAttr theFile, vbNormal
    On Error GoTo eh
    Kill theFile
Exit Function
eh:
    SafeKill = Err.Number & " " & Err.Description
End Function



⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?