Const FILE_SHARE_READ = &O1
Const FILE_SHARE_WRITE = &O2
Const GENERIC_ALL = &H10000000
Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000
Const OPEN_EXISTING = 3
Const INVALID_HANDLE_VALUE = -1
Private Type DateFichier
CreationTime As Date
LastAccessTime As Date
LastWriteTime As Date
End Type
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwSharedMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile 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 Currency, lpLastAccessTime As Currency, lpLastWriteTime As Currency) As Long
Private Function GetDateFichier(FileName As String) As DateFichier
Dim dt1 As Currency
Dim dt2 As Currency
Dim dt3 As Currency
Dim dt As Date
Dim hCrea As Long
hCrea = CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
If hCrea = INVALID_HANDLE_VALUE Then
MsgBox "Erreur Lecture: " & FileName
Exit Function
End If
' renvoie le nombre de 100-nanosecond depuis le 1er Janvier 1601
GetFileTime hCrea, dt1, dt2, dt3
CloseHandle (hCrea)
' conversion en date pour Vb
dt = (dt1 / 1000 - 9435312000#) / 86400
GetDateFichier.CreationTime = dt
dt = (dt2 / 1000 - 9435312000#) / 86400
GetDateFichier.LastAccessTime = dt
dt = (dt3 / 1000 - 9435312000#) / 86400
GetDateFichier.LastWriteTime = dt
End Function
' exemple d'utilisation
Dim dat As DateFichier
Dim s As String
dat = GetDateFichier("C:\autoexec.bat")
s = "date création........." & dat.CreationTime & vbCrLf
s = s & "date modification...." & dat.LastWriteTime & vbCrLf
s = s & "date dernier accès.." & dat.LastAccessTime
MsgBox s