用Delphi如何读写文件的创建、修改、访问日期和时间
获得最后使用文件的日期:
function GetFileLastAccessTime(sFileName:string;uFlag:byte):TDateTime;
var
ffd:TWin32FindData;
dft:DWord;
lft:TFileTime;
h:THandle;
begin
h:=FindFirstFile(PChar(sFileName),ffd);
if h<>INVALID_HANDLE_VALUE then
begin
case uFlag of
FILE_CREATE_TIME:FileTimeToLocalFileTime(ffd.ftCreationTime,lft);
FILE_MODIFY_TIME:FileTimeToLocalFileTime(ffd.ftLastWriteTime,lft);
FILE_ACCESS_TIME:FileTimeToLocalFileTime(ffd.ftLastAccessTime,lft);
else
FileTimeToLocalFileTime(ffd.ftLastAccessTime,lft);
end;
FileTimeToDosDateTime(lft,LongRec(dft).Hi,LongRec(dft).Lo);
Result:=FileDateToDateTime(dft);
windows.FindClose(h);
end
else
result:=0;
end;
GetFileLastAccessTime()将会以 Delphi的 TdateTi
me格式返回你所指定的文件的最后访问日期
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
OpenDialog1: TOpenDialog;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
Var Q : Integer;
ST : TSystemTime;
FT : TFileTime;
begin
ST.wMonth := 12;
ST.wDayOfWeek := 0;
ST.wDay := 23;
ST.wYear := 1+2000;
ST.wHour := 1;
ST.wMinute := 1;
ST.wSecond := 1;
ST.wMilliseconds := 0;
SystemTimeToFileTime(ST, FT);
Q := FileOpen('D:\delphi_study\AVIWRITER.zip', fmOpenWrite or fmShareDenyNone); ;
SetFileTime(Q, @FT, @FT, @FT) ;
FileClose(Q);
end;
procedure TForm1.Button2Click(Sender: TObject);
function GetFileDate(TheFileName: string): string;
var
FHandle: integer;
begin
FHandle := FileOpen(thefilename, 0);
try
Result := DateTimeToStr(FileDateToDateTime(FileGetDate(FHandle)));
finally
FileClose(FHandle);
end;
end;
begin
showmessage('D:\delphi_study\AVIWRITER.zip'+'时间属性为'+getfiledate('D:\delphi_study\AVIWRITER.zip'))
end;
end.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。