修改文件的创建日期、访问日期、最后修改日期

可以使用api

SetFileTime

VB声明
Declare Function SetFileTime Lib "kernel32" Alias "SetFileTime" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
说明
设置文件的创建、访问及上次修改时间
返回值
Long,非零表示成功,零表示失败。会设置GetLastError
参数表
参数 类型及说明
hFile Long,系统文件句柄
lpCreationTime FILETIME,文件的创建时间
lpLastAccessTime FILETIME,文件上一次访问的时间
lpLastWriteTime FILETIME,文件最近一次修改的时间

Filetime是一个结构,定义如下.
类型定义
Type FILETIME ' 8 Bytes
dwLowDateTime As Long
dwHighDateTime As Long
End Type
说明
windows提供了一种特殊的机制,可以记录文件的访问及创建时间。在win32环境中,这些信息以64位值的形式保存,量度的是自1601年1月1日以来经历的100ns时间单位数量(64-bit number specifying the elapsed time since January 1, 1601, in 100-nanosecond increments.)
注解
文件时间在系统中通常用“协同世界时间”(UTC)的格式保存,但同时提供了在UTC及本地时间之间转换的函数。FILETIME结构里可包含UTC或本地时间——由我们自行决定在结构中包含什么时间

下面是一个实例
'This project needs a Common Dialog box, named CDBox.
' (To add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
' and select Microsoft Common Dialog control)
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 Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function SetFileTime Lib "kernel32" (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 CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Private Sub Form_Load()
'KPD-Team 1998
'URL: http://www.allapi.net/
'KPDTeam@Allapi.net
Dim m_Date As Date, lngHandle As Long
Dim udtFileTime As FILETIME
Dim udtLocalTime As FILETIME
Dim udtSystemTime As SYSTEMTIME
m_Date = Format(Now, "DD-MM-YY")

'Set the dialog's title
CDBox.DialogTitle = "Choose a file ..."
'Set the dialog's filter
CDBox.Filter = "All Files (*.*)|*.*"
'Show the 'Open File'-dialog
CDBox.ShowOpen

udtSystemTime.wYear = Year(m_Date)
udtSystemTime.wMonth = Month(m_Date)
udtSystemTime.wDay = Day(m_Date)
udtSystemTime.wDayOfWeek = WeekDay(m_Date) - 1
udtSystemTime.wHour = Hour(m_Date)
udtSystemTime.wSecond = Second(m_Date)
udtSystemTime.wMilliseconds = 0

' convert system time to local time
SystemTimeToFileTime udtSystemTime, udtLocalTime
' convert local time to GMT
LocalFileTimeToFileTime udtLocalTime, udtFileTime
' open the file to get the filehandle
lngHandle = CreateFile(CDBox.Filename, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
' change date/time property of the file
SetFileTime lngHandle, udtFileTime, udtFileTime, udtFileTime
' close the handle
CloseHandle lngHandle
MsgBox "The date of the file '" + CDBox.Filename + "' has been changed to" + Str$(m_Date), vbInformation + vbOKOnly, App.Title
End Sub

Top

3 楼cuilonggang(白金)回复于 2005-03-12 20:39:57 得分 0 可以使用api

SetFileTime

VB声明
Declare Function SetFileTime Lib "kernel32" Alias "SetFileTime" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
说明
设置文件的创建、访问及上次修改时间
返回值
Long,非零表示成功,零表示失败。会设置GetLastError
参数表
参数 类型及说明
hFile Long,系统文件句柄
lpCreationTime FILETIME,文件的创建时间
lpLastAccessTime FILETIME,文件上一次访问的时间
lpLastWriteTime FILETIME,文件最近一次修改的时间

Filetime是一个结构,定义如下.
类型定义
Type FILETIME ' 8 Bytes
dwLowDateTime As Long
dwHighDateTime As Long
End Type
说明
windows提供了一种特殊的机制,可以记录文件的访问及创建时间。在win32环境中,这些信息以64位值的形式保存,量度的是自1601年1月1日以来经历的100ns时间单位数量(64-bit number specifying the elapsed time since January 1, 1601, in 100-nanosecond increments.)
注解
文件时间在系统中通常用“协同世界时间”(UTC)的格式保存,但同时提供了在UTC及本地时间之间转换的函数。FILETIME结构里可包含UTC或本地时间——由我们自行决定在结构中包含什么时间

下面是一个实例
'This project needs a Common Dialog box, named CDBox.
' (To add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
' and select Microsoft Common Dialog control)
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 Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function SetFileTime Lib "kernel32" (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 CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Private Sub Form_Load()
'KPD-Team 1998
'URL: http://www.allapi.net/
'KPDTeam@Allapi.net
Dim m_Date As Date, lngHandle As Long
Dim udtFileTime As FILETIME
Dim udtLocalTime As FILETIME
Dim udtSystemTime As SYSTEMTIME
m_Date = Format(Now, "DD-MM-YY")

'Set the dialog's title
CDBox.DialogTitle = "Choose a file ..."
'Set the dialog's filter
CDBox.Filter = "All Files (*.*)|*.*"
'Show the 'Open File'-dialog
CDBox.ShowOpen

udtSystemTime.wYear = Year(m_Date)
udtSystemTime.wMonth = Month(m_Date)
udtSystemTime.wDay = Day(m_Date)
udtSystemTime.wDayOfWeek = WeekDay(m_Date) - 1
udtSystemTime.wHour = Hour(m_Date)
udtSystemTime.wSecond = Second(m_Date)
udtSystemTime.wMilliseconds = 0

' convert system time to local time
SystemTimeToFileTime udtSystemTime, udtLocalTime
' convert local time to GMT
LocalFileTimeToFileTime udtLocalTime, udtFileTime
' open the file to get the filehandle
lngHandle = CreateFile(CDBox.Filename, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
' change date/time property of the file
SetFileTime lngHandle, udtFileTime, udtFileTime, udtFileTime
' close the handle
CloseHandle lngHandle
MsgBox "The date of the file '" + CDBox.Filename + "' has been changed to" + Str$(m_Date), vbInformation + vbOKOnly, App.Title
End Sub

Top

4 楼MY2000(DESTINY:非[版务].灌!)回复于 2005-03-12 20:52:19 得分 0 为什么回复两次?
Top

5 楼jhtyx(jhtyx)回复于 2005-03-24 12:32:15 得分 0 这个方法不一定可行,有时行,有时不行。再说了过于复杂
Top

6 楼junki(『.NET技术争霸天下』)回复于 2005-03-24 13:09:28 得分 5有没有打开它的权限
Top

7 楼jhtyx(jhtyx)回复于 2005-03-31 11:59:33 得分 0 有打开的权限,文件是程序建立的,程序将文件建好后,再将文件的日期更改。
Top

8 楼xinliangyu(yxl)回复于 2005-03-31 15:46:23 得分 10看我写的:
Const OFS_MAXPATHNAME = 128
Const OF_READ = &H0

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

Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle 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 Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Sub ShowFileTime(sFileFullName As String)
Dim lngHandle As Long
Dim Ft1 As FILETIME, Ft2 As FILETIME
Dim Ft3 As FILETIME, SysTime As SYSTEMTIME
Dim OF As OFSTRUCT
Dim strTime As String

lngHandle = OpenFile(sFileFullName, OF, OF_READ)

GetFileTime lngHandle, Ft1, Ft2, Ft3
FileTimeToSystemTime Ft1, SysTime
strTime = "创建时间:" & Trim(SysTime.wYear) & "-" & _
Trim(SysTime.wMonth) & "-" & _
LTrim(SysTime.wDay) & " " & _
Trim(SysTime.wHour) & ":" & _
Trim(SysTime.wMinute) & ":" & _
Trim(SysTime.wSecond)

FileTimeToSystemTime Ft2, SysTime
strTime = strTime & vbNewLine & "最近访部:" & Trim(SysTime.wYear) & "-" & _
Trim(SysTime.wMonth) & "-" & _
LTrim(SysTime.wDay) & " " & _
Trim(SysTime.wHour) & ":" & _
Trim(SysTime.wMinute) & ":" & _
Trim(SysTime.wSecond)

FileTimeToSystemTime Ft3, SysTime
strTime = strTime & vbNewLine & "最后修改:" & Trim(SysTime.wYear) & "-" & _
Trim(SysTime.wMonth) & "-" & _
LTrim(SysTime.wDay) & " " & _
Trim(SysTime.wHour) & ":" & _
Trim(SysTime.wMinute) & ":" & _
Trim(SysTime.wSecond)
CloseHandle lngHandle

MsgBox strTime
End Sub

Top

9 楼xinliangyu(yxl)回复于 2005-03-31 15:47:29 得分 0 对不起,你要的是“修改”!
就当我没有说。

加支付宝好友偷能量挖...


评论(0)网络
阅读(149)喜欢(0)Asp/VBScript