修改文件时间

=====================================================================================
Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
【说明】
根据一个FILETIME结构的内容,载入一个SYSTEMTIME结构
【返回值】
Long,非零表示成功,零表示失败。会设置GetLastError
【参数表】
lpSystemTime - SYSTEMTIME,包含了系统时间信息的一个结构
lpFileTime - FILETIME,用于装载文件时间的一个结构
=====================================================================================
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
【说明】
将本时区的文件时间转换成格林威治时间的文件时间。
=====================================================================================
Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
【说明】
【参数表】
lpFileName - String,欲打开文件的名字
lpReOpenBuff - OFSTRUCT,该结构填充的数据包括与文件和操作结果有关的信息
wStyle - Long,参考表OpenFile-1(OpenFile函数的标志常数表)总结的标志常数的组合,它决定了要采取的操作方式
  表OpenFile-1(OpenFile函数的标志常数表)
  wStyle常数
=====================================================================================
Private Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
【说明】
设置文件创建时间、访问时间及修改时间
【返回值】
Long,非零表示成功,零表示失败。
【参数表】
hFile - Long,文件句柄
lpCreationTime - FILETIME,文件的创建时间
lpLastAccessTime - FILETIME,文件的访问时间
lpLastWriteTime - FILETIME,文件的修改时间
=====================================================================================
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
【说明】
关闭一个内核对象。其中包括文件、文件映射、进程、线程、安全和同步对象等。涉及文件处理时,这个函数通常与vb的close命令相似。应尽可能的使用close,因为它支持vb的差错控制。注意这个函数使用的文件句柄与vb的文件编号是完全不同的
【返回值】
Long,非零表示成功,零表示失败。
【备注】
除非对内核对象的所有引用都已关闭,否则该对象不会实际删除
【参数表】
hObject - Long,欲关闭的一个对象的句柄
=====================================================================================
Option Explicit
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 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 LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Private Const OF_READWRITE = &H2
Private Const OFS_MAXPATHNAME = 128
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 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 Sub Form_Load()
    ChangeFileTime "c:/123.txt"
    Unload Me
End Sub

Private Sub ChangeFileTime(StrFile As String)
    Dim CreationSysTime As SYSTEMTIME
    Dim AccessSysTime As SYSTEMTIME
    Dim ChangeSysTime As SYSTEMTIME
    Dim LocalCreationFileTime As FILETIME
    Dim LocalAccessFileTime As FILETIME
    Dim LocalChangeFiletime As FILETIME
    Dim CreationFileTime As FILETIME
    Dim AccessFileTime As FILETIME
    Dim ChangeFileTime As FILETIME
    Dim hFile As Long
    Dim fStruct As OFSTRUCT
    With CreationSysTime '2005年11月20日,12:10:20
        .wYear = 2005
        .wMonth = 11
        .wDay = 20
        .wHour = 12
        .wMinute = 10
        .wSecond = 20
    End With
    With AccessSysTime '2006年10月7日,16:42:13
        .wYear = 2006
        .wMonth = 10
        .wDay = 7
        .wHour = 16
        .wMinute = 42
        .wSecond = 13
    End With
    With ChangeSysTime '2005年4月4日, 13:41:30
        .wYear = 2005
        .wMonth = 4
        .wDay = 4
        .wHour = 13
        .wMinute = 41
        .wSecond = 30
    End With
    SystemTimeToFileTime CreationSysTime, LocalCreationFileTime
    LocalFileTimeToFileTime LocalCreationFileTime, CreationFileTime
    SystemTimeToFileTime AccessSysTime, LocalAccessFileTime
    LocalFileTimeToFileTime LocalAccessFileTime, AccessFileTime
    SystemTimeToFileTime ChangeSysTime, LocalChangeFiletime
    LocalFileTimeToFileTime LocalChangeFiletime, ChangeFileTime
    hFile = OpenFile(StrFile, fStruct, OF_READWRITE)
    SetFileTime hFile, CreationFileTime, AccessFileTime, ChangeFileTime
    CloseHandle hFile
End Sub 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值