unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls; type TForm1 = class(TForm) DateTimePicker1: TDateTimePicker; DateTimePicker2: TDateTimePicker; DateTimePicker3: TDateTimePicker; DateTimePicker4: TDateTimePicker; Button1: TButton; Label1: TLabel; Label2: TLabel; OpenDialog1: TOpenDialog; Edit1: TEdit; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} function CovFileDate(Fd:_FileTime):TDateTime; // 转换文件的时间格式 var Tct:_SystemTime; Temp:_FileTime; begin FileTimeToLocalFileTime(Fd,Temp); FileTimeToSystemTime(Temp,Tct); CovFileDate:=SystemTimeToDateTime(Tct); end; procedure GetFileTime(const Tf:string); // 得到文件时间,输入值表示目标文件的路径 const Model='yyyy/mm/dd,hh:mm:ss'; // 这是时间的格式 var Tp:TSearchRec; //申明一个查找记录 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; procedure SetFileDateTime(const Tf:string); //设置文件时间,输入值表示目标文件的路径 var Dt1,Dt2:Integer; Fs:TFileStream; Fct,Flt:TFileTime; begin Dt1:=DateTimeToFileDate(Trunc(Form1.DateTimePicker1.Date) + Frac(Form1.DateTimePicker2.Time)); Dt2:=DateTimeToFileDate(Trunc(Form1.DateTimePicker3.Date) + Frac(Form1.DateTimePicker4.Time)); //以上转换用户输入在DataTimePicker中的信息,Form1.DateTimePicker1.Date和Form1.DateTimePicker2.Time就是创建时间,Form1.DateTimePicker3.Date和Form1.DateTimePicker4.Time就是修改时间 try FS := TFileStream.Create(Tf, fmOpenReadWrite); //创建TFileStream try if DosDateTimeToFileTime(LongRec(DT1).Hi, LongRec(DT1).Lo, Fct) and LocalFileTimeToFileTime(Fct, Fct) and DosDateTimeToFileTime(LongRec(DT2).Hi, LongRec(DT2).Lo, Flt) and LocalFileTimeToFileTime(Flt, Flt) then SetFileTime(FS.Handle,@Fct, @Flt, @Flt); // 设置文件时间属性 finally FS.Free; //释放 end; MessageDlg('修改成功',mtconfirmation, [mbOk], 0); //提示成功 except MessageDlg('修改失败',mtError, [mbOk], 0); //因为目标文件正在被使用等原因而导致失败 end; end; procedure TForm1.Button1Click(Sender: TObject); begin SetFileDateTime(edit1.text); //按下开始就设置文件的时间 end; procedure TForm1.Button2Click(Sender: TObject); begin if OpenDialog1.Execute then Edit1.Text:=OpenDialog1.FileName; //让用户选择某个文件 end; end.