DateTimeToFileDate不起作用

时间:2012-11-25 11:08:10

标签: delphi delphi-xe2

我有这段代码:

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.

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

1 个答案:

答案 0 :(得分:2)

您在TFile上调用的方法需要TDateTime。请删除DateTimeToFileDate来电。

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

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

TFIle.SetCreationTime(fFileName, EncodeDateTime(1981, 10, 5, 20, 35, 40, 10));