下面是我的代码
Public Function SetFileTimeCrated(StrPathName As String, FileSystemTime As SYSTEMTIME) As Boolean
'设置文件的创建时间
Dim Retval As Long, hFile As Long
Dim OF As OFSTRUCT
Dim CreatedFT As FILETIME, AccessedFT As FILETIME, ModifiedFT As FILETIME
Dim udtSystemTime As SYSTEMTIME
Dim udtLocalTime As FILETIME
Dim udtFileTime As FILETIME
hFile = OpenFile(StrPathName, OF, OF_READWRITE Or OF_SHARE_DENY_NONE) '这样可以对自身的程序的日期进行修改,不能实现对API相关DLL的日期的修改
' hFile = CreateFile(StrPathName, GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0, 0&)
'hFile = CreateFile(StrPathName, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0, 0&)
If hFile = 0 Then
'If hFile = -1 Then
MsgBox "系统文件出错"
Exit Function
End If
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
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Declare Function GetFileTime Lib "kernel32" (ByVal hfile As Long, lpCreationTime As Any, lpLastAccessTime As Any, lpLastWriteTime As Any) As Long
Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Private Declare Function VariantTimeToSystemTime Lib "OLEAUT32.DLL" (ByVal vtime As Double, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function SystemTimeToVariantTime Lib "OLEAUT32.DLL" (lpSystemTime As SYSTEMTIME, vtime As Double) As Long
Public Function FileCreationTime(ByVal hfile As Long) As Date
Dim bSuccess As Boolean
Dim dtFileTime As FILETIME
' Fetch time stamp from open file
bSuccess = GetFileTime(hfile, dtFileTime, ByVal 0&, ByVal 0&)
' Convert FILETIME to a VB date value
FileCreationTime = FileTimeToDate(dtFileTime)
End Function
Public Function FileTimeToDate( _
FILETIME As FILETIME, _
Optional ByVal ConvertToLocal As Boolean = True) As Date
'===========================================================================
' FileTimeToDate - Converts FILETIME structure to a VB Date data type.
'
' NOTE: The FILETIME structure is a structure of 100-nanosecond intervals
' since January 1, 1601. The VB Date data type is a floating point value
' where the value to the left of the decimal is the number of days since
' December 30, 1899, and the value to the right of the decimal represents
' the time.
'
' FileTime The FILETIME structure to convert.
' ConvertToLocal Optional. True to convert from UTC to local time.
'
' RETURNS A date/time value in the intrinsic VB Date data type.
'
'===========================================================================
Dim Success As Boolean
Dim SysTime As SYSTEMTIME
Dim ftdouble As Double
' Convert to FILETIME
Success = FileTimeToSystemTime(FILETIME, SysTime)
If Success Then
' Convert to a Variant date
Success = SystemTimeToVariantTime(SysTime, ftdouble)
End If
If Not Success Then
Err.Raise Err.Number, Err.Source, Err.Description
End If
Private Declare Function GetDesktopWindow Lib "user32.dll" () As Long
Private Declare Function IsWindowUnicode Lib "user32.dll" (ByVal hwnd As Long) As Long
Public Function GetFileHandle(ByVal FileName As String, bOpen As Boolean) As Long
' Function uses APIs to read/create files with unicode support
Const GENERIC_READ As Long = &H80000000
Const OPEN_EXISTING = &H3
Const FILE_SHARE_READ = &H1
Const GENERIC_WRITE As Long = &H40000000
Const FILE_SHARE_WRITE As Long = &H2
Const CREATE_ALWAYS As Long = 2
Const FILE_ATTRIBUTE_ARCHIVE As Long = &H20
Const FILE_ATTRIBUTE_HIDDEN As Long = &H2
Const FILE_ATTRIBUTE_READONLY As Long = &H1
Const FILE_ATTRIBUTE_SYSTEM As Long = &H4
Dim Flags As Long, Access As Long
Dim Disposition As Long, Share As Long
Dim bISNT As Boolean
bISNT = (Not (IsWindowUnicode(GetDesktopWindow) = 0&))
If bOpen Then
Access = GENERIC_READ
Share = FILE_SHARE_READ
Disposition = OPEN_EXISTING
Flags = FILE_ATTRIBUTE_ARCHIVE Or FILE_ATTRIBUTE_HIDDEN Or FILE_ATTRIBUTE_NORMAL _
Or FILE_ATTRIBUTE_READONLY Or FILE_ATTRIBUTE_SYSTEM
Else
Access = GENERIC_READ Or GENERIC_WRITE
Share = 0&
If bISNT Then
Flags = GetFileAttributesW(StrPtr(FileName))
Else
Flags = GetFileAttributesA(FileName)
End If
If Flags < 0& Then Flags = FILE_ATTRIBUTE_NORMAL
' CREATE_ALWAYS will delete previous file if necessary
Disposition = CREATE_ALWAYS
End If
If bISNT Then
GetFileHandle = CreateFileW(StrPtr(FileName), Access, Share, ByVal 0&, Disposition, Flags, 0&)
Else
GetFileHandle = CreateFileA(FileName, Access, Share, ByVal 0&, Disposition, Flags, 0&)
End If
If GetFileHandle = 0 Then
If isStringANSI(FileName) Then
' hFile should never be zero. It should be -1 (error) or a valid handle
' when hFile is zero, most likely API was called on a Win9x system
' so we will call the ANSI version and see if that returns a handle
GetFileHandle = CreateFileA(FileName, Access, Share, ByVal 0&, Disposition, Flags, 0&)
End If
End If
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.dll" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function FileTimeToLocalFileTime Lib "kernel32.dll" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Private Declare Function SetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME) As Long
Private Const OFS_MAXPATHNAME = 128
Private Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName As String * OFS_MAXPATHNAME
End Type
Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
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
'时区
Private Type TIME_ZONE_INFORMATION
Bias As Long
StandardName(32) As Integer
StandardDate As SYSTEMTIME
StandardBias As Long
DaylightName(32) As Integer
DaylightDate As SYSTEMTIME
DaylightBias As Long
End Type
Function GetFileTimeCreated(ByVal StrPathName As String) As SYSTEMTIME
'文件的创建时间
Dim Retval As Long, hFile As Long
Dim OF As OFSTRUCT
Dim CreatedFT As FILETIME, AccessedFT As FILETIME, ModifiedFT As FILETIME
Dim udtSystemTime As SYSTEMTIME
Dim udtLocalTime As FILETIME
Dim udtFileTime As FILETIME
hFile = OpenFile(StrPathName, OF, OF_WRITE Or OF_SHARE_DENY_NONE)
If hFile = 0 Then
MsgBox "系统文件出错"
Exit Function
End If
下面为测试代码
Dim SysCreatTime As SYSTEMTIME
Dim StrDate As String
'SysCreatTime = GetFileTimeCreated("C:\test.txt") '读取这个能正常
SysCreatTime = GetFileTimeCreated("C:\WINDOWS\system32\user32.dll") '结果错误 显示为1601-1-1
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.dll" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function FileTimeToLocalFileTime Lib "kernel32.dll" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Private Declare Function SetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME) As Long
Private Const OFS_MAXPATHNAME = 128
Private Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName As String * OFS_MAXPATHNAME
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
'时区
Private Type TIME_ZONE_INFORMATION
Bias As Long
StandardName(32) As Integer
StandardDate As SYSTEMTIME
StandardBias As Long
DaylightName(32) As Integer
DaylightDate As SYSTEMTIME
DaylightBias As Long
End Type
Private Function GetFileTimeCreated(ByVal StrPathName As String) As SYSTEMTIME
'文件的创建时间
Dim Retval As Long, hFile As Long
Dim OF As OFSTRUCT
Dim CreatedFT As FILETIME, AccessedFT As FILETIME, ModifiedFT As FILETIME
Dim udtSystemTime As SYSTEMTIME
Dim udtLocalTime As FILETIME
Dim udtFileTime As FILETIME
' hFile = OpenFile(StrPathName, OF, OF_WRITE Or OF_SHARE_DENY_NONE)
hFile = CreateFile(StrPathName, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0, 0&)
If hFile = 0 Then
MsgBox "系统文件出错"
Exit Function
End If
Dim SysCreatTime As SYSTEMTIME
Dim StrDate As String
'SysCreatTime = GetFileTimeCreated("C:\test.txt") '读取这个能正常
SysCreatTime = GetFileTimeCreated("C:\WINDOWS\system32\user32.dll") '结果错误 显示为1601-1-1
StrDate = SysCreatTime.wYear & "-" & SysCreatTime.wMonth & "-" & SysCreatTime.wDay
MsgBox StrDate
End Sub