1.如何获得本机Windows系统中一个文件的创建时间或最后修改时间
2.如何删除本机Windows系统中一个文件

解决方案 »

  1.   

    2:DeleteFile(LPCTSTR lpFileName);  // pointer to name of file to delete  
      

  2.   

    1.如何获得本机Windows系统中一个文件的创建时间或最后修改时间
    var
      fileName   : string;
      fileDate   : Integer;begin
      // Try to open the Unit1.DCU file for the current project
      fileName := 'Unit1.DCU';
      fileDate := FileAge(fileName);  // Did we get the file age OK?
      if fileDate > -1 then
        ShowMessage(fileName+' last modified date = '+
                    DateToStr(FileDateToDateTime(fileDate)));
    end;
    2.如何删除本机Windows系统中一个文件
    var
      fileName : string;
      myFile   : TextFile;
      data     : string;begin
      // Try to open a text file for writing to
      fileName := 'Test.txt';
      AssignFile(myFile, fileName);
      ReWrite(myFile);  // Write to the file
      Write(myFile, 'Hello World');  // Close the file
      CloseFile(myFile);  // Reopen the file in read mode
      Reset(myFile);  // Display the file contents
      while not Eof(myFile) do
      begin
        ReadLn(myFile, data);
        ShowMessage(data);
      end;  // Close the file for the last time
      CloseFile(myFile);  // Now delete the file
      if DeleteFile(fileName)
      then ShowMessage(fileName+' deleted OK')
      else ShowMessage(fileName+' not deleted');  // Try to delete the file again
      if DeleteFile(fileName)
      then ShowMessage(fileName+' deleted OK again!')
      else ShowMessage(fileName+' not deleted, error = '+
                       IntToStr(GetLastError));
    end;
      

  3.   

    1.
    function GetFileCreationTime(const Filename: string): TDateTime;
    var
      Data: TWin32FindData;
      H: THandle;
      FT: TFileTime;
      I: Integer;
    begin
      H := FindFirstFile(PCHAR(Filename), Data);
      if H <> INVALID_HANDLE_VALUE then begin
        try
          FileTimeToLocalFileTime(Data.ftLastWriteTime, FT);  {换此处参数}
          FileTimeToDosDateTime(FT, LongRec(I).Hi, LongRec(I).Lo);
          Result := FileDateToDateTime(I);
        finally
          Windows.FindClose(H);
        end
      end else begin
        Result := 0;
      end;
    end;------
    Data.ftLastWriteTime  {最后修改}
         ftCreationTime: TFileTime;  {创建}
         ftLastAccessTime: TFileTime;{最后访问}
      

  4.   

    问题一:转主  题:  怎样取得文件的日期时间信息? 
    在单元开头的 uses 子句中加上对 
    Sysutils 单元的引用?FileDateToDateTime 和 DateTimeToFileDate 这两个函数是在
    该单元中定义的写了两个函数:
    type
      TFileTimeType = (fttCreation, fttLastAccess, fttLastWrite);
      //分别对应文件创建时间,访问时间,修改时间
    function GetFileDateTime(const FileName: string; FileTimeType: TFileTimeType): TDateTime;
    var     
      Handle: THandle;
      FindData: TWin32FindData;
      LocalFileTime: TFileTime;
      DosDateTime: Integer;
    begin
      Handle := FindFirstFile(PChar(FileName), FindData);
      if Handle <> INVALID_HANDLE_VALUE then
      begin
        Windows.FindClose(Handle);
        if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
        begin
          case FileTimeType of
          fttCreation:
            FileTimeToLocalFileTime(FindData.ftCreationTime, LocalFileTime);
          fttLastAccess:
            FileTimeToLocalFileTime(FindData.ftLastAccessTime, LocalFileTime);
          fttLastWrite:
            FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);
          end;
          if FileTimeToDosDateTime(LocalFileTime, LongRec(DosDateTime).Hi,
            LongRec(DosDateTime).Lo) then 
          begin
            Result := FileDateToDateTime(DosDateTime);
            Exit;
          end;
        end;
      end;
      Result := -1;end;function SetFileDateTime(const FileName: string; FileTimeType: TFileTimeType; DateTime: TDateTime): Integer;
    var
      Handle: THandle;
      LocalFileTime, FileTime: TFileTime;
      DosDateTime: Integer;
      I : TFileTimeType;
      FileTimes: array[TFileTimeType] of Pointer;
    begin
      Result := 0;
      DosDateTime := DateTimeToFileDate(DateTime);
      Handle := FileOpen(FileName, fmOpenWrite or fmShareDenyNone);
      if Handle <> INVALID_HANDLE_VALUE then
      begin
        for I := fttCreation to fttLastWrite do
          FileTimes[I] := nil;
        DosDateTimeToFileTime(LongRec(DosDateTime).Hi, LongRec(DosDateTime).Lo, LocalFileTime);
        LocalFileTimeToFileTime(LocalFileTime, FileTime);
        FileTimes[FileTimeType] := @FileTime;
        if SetFileTime(Handle, FileTimes[fttCreation], FileTimes[fttLastAccess],
          FileTimes[fttLastWrite]) then Exit;
      end;
      Result := GetLastError;end;使用:
    1、获取文件创建时间:
       ShowMessage(DateTimeToStr(GetFileDateTime('c:\key.txt',fttLastWrite)));
    2、设置文件修改时间:
       SetFileDateTime('c:\key.txt',fttLastWrite, datetimepicker1.DateTime); 问题二
     DeleteFile(LPCTSTR lpFileName); 
      
     
      

  5.   

    我的方法最简单:
    1、打开资源管理器-》在文件上点击右键-》选择【属性】-》可以得到该文件的创建时间和修改时间!
    2、打开资源管理器-》选中文件-》按下键盘上的【Delete】键或者点选右键菜单上的【删除】菜单项-》可以删除这个文件!=========================================
    多么简单明了呀!
      

  6.   

    to:yirnoo:太牛了……佩服佩服
      

  7.   

    1.
    function GetFileCreationTime(const Filename: string): TDateTime;
    var
      Data: TWin32FindData;
      H: THandle;
      FT: TFileTime;
      I: Integer;
    begin
      H := FindFirstFile(PCHAR(Filename), Data);
      if H <> INVALID_HANDLE_VALUE then begin
        try
          FileTimeToLocalFileTime(Data.ftLastWriteTime, FT);  {换此处参数}
          FileTimeToDosDateTime(FT, LongRec(I).Hi, LongRec(I).Lo);
          Result := FileDateToDateTime(I);
        finally
          Windows.FindClose(H);
        end
      end else begin
        Result := 0;
      end;
    end;------
    Data.ftLastWriteTime  {最后修改}
         ftCreationTime: TFileTime;  {创建}
         ftLastAccessTime: TFileTime;{最后访问}
      

  8.   

    不好意思,来接分了。
    if FileExists(s) then DeleteFile(s);
      

  9.   

    to:yirnoo 
     够狠。
      

  10.   

    to:
      sunnysongwang(sunnysong)!
      fim(镇江DJ)(DJ=DelphiJava)!
      dami_1(dami_1)
      YOHOYOHO(月光下的军鸽)
    =========================================
      哈哈!
      承蒙夸奖!愧不敢当!
      楼主也没有说非要编程得到上述信息!一时手痒!就回了一贴!
      独乐乐不如众乐乐!搏大家一笑又有何妨!