filegetdate()只能获取文件修改时间?!

解决方案 »

  1.   

    这段函数是不是你需要的,摘自hudong的葵花宝典
    procdeure GetFileTime(const Tf:string);
    { 获取文件时间,Tf表示目标文件路径和名称 }
    const
    Model='yyyy/mm/dd,hh:mm:ss'; { 设定时间格式 }
    var
    Tp:TSearchRec; { 申明Tp为一个查找记录 }
    T1,T2,T3:string;begin
    FindFirst(Tf,faAnyFile,Tp); { 查找目标文件 } T1:=FormatDateTime(Model,
    CovFileDate(Tp.FindData.ftCreationTime)));
    { 返回文件的创建时间 }
    T2:=FormatDateTime(Model,
    CovFileDate(Tp.FindData.ftLastWriteTime)));
    { 返回文件的修改时间 }
    T3:=FormatDateTime(Model,Now));
    { 返回文件的当前访问时间 }
    FindClose(Tp);
    end;
      

  2.   

    function Tmainform.CovFileDate(Fd: _FileTime): TDateTime;
    var
      Tct:_SystemTime;
      Temp:_FileTime;
    begin
      FileTimeToLocalFileTime(Fd,Temp);
      FileTimeToSystemTime(Temp,Tct);
      CovFileDate:=SystemTimeToDateTime(Tct);
    end;procedure Tmainform.getfiletime(const tf: string);
    const
     Model='dddddd-dddd,hh:mm:ssss';
    var
     Tp:TSearchRec;
    begin
     FindFirst(Tf,faAnyFile,Tp);
     T2:=FormatDateTime(Model,CovFileDate(Tp.FindData.ftLastWriteTime));
     Findclose(tp);
    end;
      

  3.   

    //==============================================================================
    //修改文件‘创建’、‘修改’日期时间********************************************
    //==============================================================================
    function SetFileDateTime(FileName: string; TargetTime: TDateTime): string;
    var FileHandle: HFile;
        SystemTime: TSystemTime;
        FileTime: TFileTime;
    begin
      DateTimeToSystemTime(TargetTime, SystemTime);
      SystemTimeToFileTime(SystemTime, FileTime);
      LocalFileTimeToFileTime(FileTime, FileTime);//将本地时间转化为系统的时间,再写入文件中
      FileHandle := FileOpen(FileName, fmOpenWrite or fmShareDenyNone);//fmOpenWrite就带有GENERIC_WRITE的意思
      if FileHandle<=0
      then Result := 'Open File Error!!!'
      else if not SetFileTime(FileHandle, @FileTime, @FileTime, @FileTime)
           then Result := 'Set File Time Error!!!'
           else Result := 'Set File Time Successfully!!!';
      FileClose(FileHandle);
    end;
      

  4.   

    [获取时间程序片段]
    var
      filehandle: Cardinal;
      ofstruct: _OFSTRUCT;
      fileinformation: _BY_HANDLE_FILE_INFORMATION;
      tempfiletime: FILETIME;
      systemtime: _SYSTEMTIME;
    begin
      if OpenDialog1.Execute then
      begin
        filehandle:=OpenFile(PChar(OpenDialog1.FileName),ofstruct,OF_READ);
        if filehandle<>HFILE_ERROR then
          begin
            if GetFileInformationByHandle(filehandle,fileinformation) then
              begin
                FileTimeToLocalFileTime(fileinformation.ftCreationTime,tempfiletime);
                FileTimeToSystemTime(tempfiletime,systemtime);
                showmessage('文件创建时间:'+inttostr(systemtime.wYear)+'年'
                                            +inttostr(systemtime.wMonth)+'月'
                                            +inttostr(systemtime.wDay)+'日,  '
                                            +inttostr(systemtime.wHour)+':'
                                            +inttostr(systemtime.wMinute)+':'
                                            +inttostr(systemtime.wSecond));
              end
            else
              showmessage('GetFileInformationByHandle Failed!');//GetLastError shows details
            CloseHandle(filehandle);
          end
        else
          showmessage('OpenFile Failed!');//may use GetLastError to show details
      end;
    end;[说明]
    程序原理:
    1.用OpenFile打开文件,获得文件句柄;
    2.用GetFileInformationByHandle通过已知的文件句柄获取文件信息,保存到文件信息结构中;
    3.用FileTimeToLocalFileTime将格林威治FILETIME转换成当地的FILETIME;
    4.用FileTimeToSystemTime将FILETIME类型的时间转换成系统时间,显示该时间;
    5.用CloseHandle关闭打开的文件句柄。
    注意的是,
    GetFileInformationByHandle函数所获得的文件信息中三种时间均是指格林威治标准时间,对应的
    系统时间应该加上中国所在的时区(+8)后得到的时间。这里采用的是直接转换的方式,没有转换成
    Large_Integer类型再去计算。更简单了。
    [设置时间程序片段]
    var
      SystemTime: _SYSTEMTIME;
      CreateTime, LastAccessTime, LastWriteTime: FileTime;
      FileHandle: Cardinal;
      ofstruct:  _OFSTRUCT;
    begin
      with SystemTime do
        try
          wYear:=StrToInt(Edit1.Text);
          wMonth:=StrToInt(Edit2.Text);
          wDay:=StrToInt(Edit3.Text);
          wHour:=StrToInt(Edit4.Text);
          wMinute:=StrToInt(Edit5.Text);
          wSecond:=StrToInt(Edit6.Text);
        except
        end;
      SystemTimeToFileTime(SystemTime,CreateTime);
      LocalFileTimeToFileTime(CreateTime,CreateTime);
      LastAccessTime:=CreateTime;//见说明
      LastWriteTime:=CreateTime; //见说明  if OpenDialog1.Execute then
      begin
        FileHandle:=OpenFile(PChar(OpenDialog1.FileName),ofstruct,OF_READWRITE);
        if FileHandle<>HFILE_ERROR then
          try
            SetFileTime(FileHandle,
                        @CreateTime,
                        @LastAccessTime,
                        @LastWriteTime);
          finally
            CloseHandle(FileHandle);
          end;
      end;
    end;{说明]
    这里设置文件的创建时间时,将LastAccessTime和LastWriteTime:=CreateTime也设成
    了跟CreateTime一样的时间,当然可以类似另设。注意文件时间有LocalFileTime与FileTime
    的区别,程序中有两句话需要注意:
    SystemTimeToFileTime(SystemTime,CreateTime);   //将系统时间转换为文件时间
    LocalFileTimeToFileTime(CreateTime,CreateTime);//将文件时间看成为当地时间并转换为文件时间
      

  5.   

    [获取时间程序片段]
    var
      filehandle: Cardinal;
      ofstruct: _OFSTRUCT;
      fileinformation: _BY_HANDLE_FILE_INFORMATION;
      tempfiletime: FILETIME;
      systemtime: _SYSTEMTIME;
    begin
      if OpenDialog1.Execute then
      begin
        filehandle:=OpenFile(PChar(OpenDialog1.FileName),ofstruct,OF_READ);
        if filehandle<>HFILE_ERROR then
          begin
            if GetFileInformationByHandle(filehandle,fileinformation) then
              begin
                FileTimeToLocalFileTime(fileinformation.ftCreationTime,tempfiletime);
                FileTimeToSystemTime(tempfiletime,systemtime);
                showmessage('文件创建时间:'+inttostr(systemtime.wYear)+'年'
                                            +inttostr(systemtime.wMonth)+'月'
                                            +inttostr(systemtime.wDay)+'日,  '
                                            +inttostr(systemtime.wHour)+':'
                                            +inttostr(systemtime.wMinute)+':'
                                            +inttostr(systemtime.wSecond));
              end
            else
              showmessage('GetFileInformationByHandle Failed!');//GetLastError shows details
            CloseHandle(filehandle);
          end
        else
          showmessage('OpenFile Failed!');//may use GetLastError to show details
      end;
    end;[说明]
    程序原理:
    1.用OpenFile打开文件,获得文件句柄;
    2.用GetFileInformationByHandle通过已知的文件句柄获取文件信息,保存到文件信息结构中;
    3.用FileTimeToLocalFileTime将格林威治FILETIME转换成当地的FILETIME;
    4.用FileTimeToSystemTime将FILETIME类型的时间转换成系统时间,显示该时间;
    5.用CloseHandle关闭打开的文件句柄。
    注意的是,
    GetFileInformationByHandle函数所获得的文件信息中三种时间均是指格林威治标准时间,对应的
    系统时间应该加上中国所在的时区(+8)后得到的时间。这里采用的是直接转换的方式,没有转换成
    Large_Integer类型再去计算。更简单了。
    [设置时间程序片段]
    var
      SystemTime: _SYSTEMTIME;
      CreateTime, LastAccessTime, LastWriteTime: FileTime;
      FileHandle: Cardinal;
      ofstruct:  _OFSTRUCT;
    begin
      with SystemTime do
        try
          wYear:=StrToInt(Edit1.Text);
          wMonth:=StrToInt(Edit2.Text);
          wDay:=StrToInt(Edit3.Text);
          wHour:=StrToInt(Edit4.Text);
          wMinute:=StrToInt(Edit5.Text);
          wSecond:=StrToInt(Edit6.Text);
        except
        end;
      SystemTimeToFileTime(SystemTime,CreateTime);
      LocalFileTimeToFileTime(CreateTime,CreateTime);
      LastAccessTime:=CreateTime;//见说明
      LastWriteTime:=CreateTime; //见说明  if OpenDialog1.Execute then
      begin
        FileHandle:=OpenFile(PChar(OpenDialog1.FileName),ofstruct,OF_READWRITE);
        if FileHandle<>HFILE_ERROR then
          try
            SetFileTime(FileHandle,
                        @CreateTime,
                        @LastAccessTime,
                        @LastWriteTime);
          finally
            CloseHandle(FileHandle);
          end;
      end;
    end;{说明]
    这里设置文件的创建时间时,将LastAccessTime和LastWriteTime:=CreateTime也设成
    了跟CreateTime一样的时间,当然可以类似另设。注意文件时间有LocalFileTime与FileTime
    的区别,程序中有两句话需要注意:
    SystemTimeToFileTime(SystemTime,CreateTime);   //将系统时间转换为文件时间
    LocalFileTimeToFileTime(CreateTime,CreateTime);//将文件时间看成为当地时间并转换为文件时间