修改文件的时间属性(VS2005)

简单的改写了一下以往的代码,调用方法

 Dim CTime As New ChangeFileTime
CTime.SetTime("D:/ABC.txt", #1/21/2007 1:15:12 PM#, True, True, True)

 

对应的时间类

''' <summary>
''' 修改文件的各类时间属性
''' 蒋玉龙修改调整VB代码于2007-07-23
''' </summary>
''' <remarks></remarks>
Public Class ChangeFileTime
    Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Integer
    Private Declare Function SystemTimeToFileTime Lib "kernel32" (ByRef lpSystemTime As SYSTEMTIME, ByRef lpFileTime As FILETIME) As Integer
    Private Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Integer, ByRef lpCreationTime As FILETIME, ByRef lpLastAccessTime As FILETIME, ByRef lpLastWriteTime As FILETIME) As Integer
    Private Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Integer, ByRef lpCreationTime As FILETIME, ByRef lpLastAccessTime As FILETIME, ByRef lpLastWriteTime As FILETIME) As Integer
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer


    Private Structure FILETIME
        Dim dwLowDateTime As Integer
        Dim dwHighDateTime As Integer
    End Structure

    Private Structure SECURITY_ATTRIBUTES
        Dim nLength As Integer
        Dim lpSecurityDescriptor As Integer
        Dim bInheritHandle As Integer
    End Structure

    Private Structure SYSTEMTIME
        Dim wYear As Short
        Dim wMonth As Short
        Dim wDayOfWeek As Short
        Dim wDay As Short
        Dim wHour As Short
        Dim wMinute As Short
        Dim wSecond As Short
        Dim wMilliseconds As Short
    End Structure

    Private Const GENERIC_READ As Integer = &H80000000
    Private Const GENERIC_WRITE As Integer = &H40000000
    Private Const FILE_SHARE_READ As Short = &H1S
    Private Const FILE_SHARE_WRITE As Short = &H2S
    Private Const OPEN_EXISTING As Short = 3
    Private Const FILE_FLAG_BACKUP_SEMANTICS As Integer = &H2000000
    Private Const INVALID_HANDLE_VALUE As Short = -1


    ''' <summary>
    ''' 更改文件的时间属性
    ''' </summary>
    ''' <param name="FilePath">文件路径</param>
    ''' <param name="SetTimeTo">更改时间</param>
    ''' <param name="ChangeCreationTime">是否修改创建时间</param>
    ''' <param name="ChangeLastAccessTime">是否修改最后访问时间</param>
    ''' <param name="ChangeLastWriteTime">是否修改最后写入时间</param>
    ''' <param name="ErrInfo">返回的错误信息</param>
    ''' <returns>修改是否成功</returns>
    ''' <remarks>蒋玉龙 2007-07-23</remarks>
    Public Function SetTime(ByVal FilePath As String, ByVal SetTimeTo As Date, Optional ByVal ChangeCreationTime As Boolean = True, Optional ByVal ChangeLastAccessTime As Boolean = False, Optional ByVal ChangeLastWriteTime As Boolean = False, Optional ByVal ErrInfo As String = "") As Boolean
        Try
            If My.Computer.FileSystem.FileExists(FilePath) = True Then
                Dim hDir As Integer
                Dim lpCreationTime As FILETIME
                Dim lpLastAccessTime As FILETIME
                Dim lpLastWriteTime As FILETIME
                Dim retval As Boolean
                Dim sAttribute As SECURITY_ATTRIBUTES
                'CreateFile:可打开和创建文件、管道、邮槽、通信服务、设备以及控制台
                hDir = CreateFile(FilePath, GENERIC_WRITE, FILE_SHARE_READ, sAttribute, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)
                If hDir = INVALID_HANDLE_VALUE Then
                    ErrInfo = "非法句柄!"
                    Return False
                Else
                    GetFileTime(hDir, lpCreationTime, lpLastAccessTime, lpLastWriteTime)
                    Dim NewTime As SYSTEMTIME = SetSystemTime(SetTimeTo)
                    If ChangeCreationTime = True Then
                        Call SystemTimeToFileTime(NewTime, lpCreationTime)
                    End If
                    If ChangeLastAccessTime = True Then
                        Call SystemTimeToFileTime(NewTime, lpLastAccessTime)
                    End If
                    If ChangeLastWriteTime = True Then
                        Call SystemTimeToFileTime(NewTime, lpLastWriteTime)
                    End If
                    retval = CBool(SetFileTime(hDir, lpCreationTime, lpLastAccessTime, lpLastWriteTime))
                    CloseHandle(hDir)
                    Return retval
                End If
            Else
                ErrInfo = "文件不存在!"
                Return False
            End If
            Return True
        Catch ex As Exception
            ErrInfo = ex.Message
            Return False
        End Try
    End Function

    Private Function SetSystemTime(ByVal SetTimeTo As Date) As SYSTEMTIME
        Dim NewTime As SYSTEMTIME
        With NewTime
            .wYear = CShort(SetTimeTo.Year)
            .wMonth = CShort(SetTimeTo.Month)
            .wDay = CShort(SetTimeTo.Day)
            .wDayOfWeek = CShort(SetTimeTo.DayOfWeek)
            .wHour = CShort(SetTimeTo.Hour)
            .wMinute = CShort(SetTimeTo.Minute)
            .wSecond = CShort(SetTimeTo.Second)
        End With
        Return NewTime
    End Function
End Class
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值