var
filehandle: Cardinal;
ofstruct: _OFSTRUCT;
fileinformation: _BY_HANDLE_FILE_INFORMATION;
tempfiletime: FILETIME;
systemtime: _SYSTEMTIME;
begin
if OpenDialog1.Execute then
begin
filehandle:=OpenFile(PChar(OpenDialog1.FileName),ofstruct,OF_READ);
if filehandle<>HFILE_ERROR then
begin
if GetFileInformationByHandle(filehandle,fileinformation) then
begin
FileTimeToLocalFileTime(fileinformation.ftCreationTime,tempfiletime);
FileTimeToSystemTime(tempfiletime,systemtime);
showmessage('文件创建时间:'+inttostr(systemtime.wYear)+'年'
+inttostr(systemtime.wMonth)+'月'
+inttostr(systemtime.wDay)+'日, '
+inttostr(systemtime.wHour)+':'
+inttostr(systemtime.wMinute)+':'
+inttostr(systemtime.wSecond));
end
else
showmessage('GetFileInformationByHandle Failed!');//GetLastError shows details
CloseHandle(filehandle);
end
else
showmessage('OpenFile Failed!');//may use GetLastError to show details
end;
end;
[设置时间程序片段]
var
SystemTime: _SYSTEMTIME;
CreateTime, LastAccessTime, LastWriteTime: FileTime;
FileHandle: Cardinal;
ofstruct: _OFSTRUCT;
begin
with SystemTime do
try
wYear:=StrToInt(Edit1.Text);
wMonth:=StrToInt(Edit2.Text);
wDay:=StrToInt(Edit3.Text);
wHour:=StrToInt(Edit4.Text);
wMinute:=StrToInt(Edit5.Text);
wSecond:=StrToInt(Edit6.Text);
except
end;
SystemTimeToFileTime(SystemTime,CreateTime);
LocalFileTimeToFileTime(CreateTime,CreateTime);
LastAccessTime:=CreateTime;//见说明
LastWriteTime:=CreateTime; //见说明
if OpenDialog1.Execute then
begin
FileHandle:=OpenFile(PChar(OpenDialog1.FileName),ofstruct,OF_READWRITE);
if FileHandle<>HFILE_ERROR then
try
SetFileTime(FileHandle,
@CreateTime,
@LastAccessTime,
@LastWriteTime);
finally
CloseHandle(FileHandle);
end;
end;
end;