試試這個。通過文件名Sub Sample()
Option Explicit
Private Declare Function OpenFile Lib "kernel32" _
(ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, _
ByVal wStyle As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Declare Function GetFileTime Lib "kernel32" _
(ByVal hFile As Long, lpCreationTime As FILETIME, _
lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function FileTimeToSystemTime Lib _
"kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Const OF_READ = &H0
Private Const OFS_MAXPATHNAME = 128
Private Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End Type
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private 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
Sub Sample()
Debug.Print GetFileCreatedDateAndTime("c:\Sample.txt")
End Sub
Private Function GetFileCreatedDateAndTime(sFile As String) As String
Dim hFile As Long, rval As Long
Dim buff As OFSTRUCT
Dim f1Time As FILETIME, f2Time As FILETIME, f3Time As FILETIME
Dim stime As SYSTEMTIME
GetFileCreatedDateAndTime = "Unable To retrieve details"
hFile = OpenFile(sFile, buff, OF_READ)
If hFile Then
rval = GetFileTime(hFile, f1Time, f2Time, f3Time)
rval = FileTimeToSystemTime(f1Time, stime)
GetFileCreatedDateAndTime = _
"Created on " & _
stime.wDay & "-" & stime.wMonth & "-" & stime.wYear & _
Chr(13) & _
"Created at " & _
stime.wHour & ":" & stime.wMinute & ":" & stime.wSecond
End If
rval = CloseHandle(hFile)
End Function
USB名稱或驅動器號? –
USB名稱...謝謝 – sharkyenergy
增加了一個替代解決方案,我原來的職位 – sharkyenergy