0

我有这个代码:

program Project1;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils, System.DateUtils, System.IOUtils;

var
  fFileName: TFilename;
  dtFileCreationTime, dtFileLastAccessTime, dtFileLastWriteTime: TTimeStamp;

begin
  try
    { TODO -oUser -cConsole Main : Insert code here }

    fFileName := 'myfilename.ext'; // here i insert correct path
    dtFileCreationTime := DateTimeToTimeStamp(EncodeDateTime(1981, 10, 5, 20,
      35, 40, 10));
    dtFileLastAccessTime := DateTimeToTimeStamp(EncodeDateTime(1982, 11, 6, 21,
      36, 41, 11));
    dtFileLastWriteTime := DateTimeToTimeStamp(EncodeDateTime(1983, 12, 7, 22,
      37, 42, 12));

    TFIle.SetCreationTime(fFileName,
      DateTimeToFileDate(TimeStampToDateTime(dtFileCreationTime)));
    TFIle.SetLastAccessTime(fFileName,
      DateTimeToFileDate(TimeStampToDateTime(dtFileLastAccessTime)));
    TFIle.SetLastWriteTime(fFileName,
      DateTimeToFileDate(TimeStampToDateTime(dtFileLastWriteTime)));

    Writeln('FileCreationTime   => ',
      DateTimeToStr(TFIle.GetCreationTime(fFileName)));
    Writeln('FileLastAccessTime => ',
      DateTimeToStr(TFIle.GetLastAccessTime(fFileName)));
    Writeln('FileLastWriteTime  => ',
      DateTimeToStr(TFIle.GetLastWriteTime(fFileName)));

  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;

end.

只是我想设置文件的日期,但它不起作用,我在哪里犯了错误?感谢帮助。

4

1 回答 1

2

您调用的方法需要TFile一个TDateTime. 所以删除DateTimeToFileDate电话。

TFIle.SetCreationTime(fFileName, TimeStampToDateTime(dtFileCreationTime));

当然,您可以删除时间戳间接。

TFIle.SetCreationTime(fFileName, EncodeDateTime(1981, 10, 5, 20, 35, 40, 10));
于 2012-11-25T11:19:53.100 回答