文件日期与delhpi日期的转换(100分)

  • 主题发起人 主题发起人 test001
  • 开始时间 开始时间
T

test001

Unregistered / Unconfirmed
GUEST, unregistred user!
文件日期与delhpi日期的转换,请举例
 
function MySetFileTime(sFileName:string;dtCreation,dtAccess,dtWrite:TDateTime):Boolean;
var
dft:DWord;
ftCreation,ftLastAccess,ftLastWrite:TFileTime;
h:THandle;
File_Attributes : Integer ;
begin
//判断文件是否为只读属性,并且更改属性
File_Attributes:=FileGetAttr(sFileName) ;
//上一句话的API实现 File_Attributes:=GetFileAttributes(pchar(sFileName)) ;
if File_Attributes and faReadOnly <> 0 then
FileSetAttr(sFileName,File_Attributes - faReadOnly) ;
//上一句话的API实现 SetFileAttributes(pchar(sFileName),File_Attributes - faReadOnly) ;

h := CreateFile(PChar(sFileName), GENERIC_WRITE, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if h<>INVALID_HANDLE_VALUE then
begin
//时间转换
dft := DateTimeToFileDate(dtCreation) ;
DosDateTimeToFileTime(LongRec(dft).Hi,LongRec(dft).Lo,ftCreation) ;
dft := DateTimeToFileDate(dtAccess) ;
DosDateTimeToFileTime(LongRec(dft).Hi,LongRec(dft).Lo,ftLastAccess) ;
dft := DateTimeToFileDate(dtWrite) ;
DosDateTimeToFileTime(LongRec(dft).Hi,LongRec(dft).Lo,ftLastWrite) ;
//更改文件的属性为之前的状态
FileSetAttr(sFileName,File_Attributes) ;
end
else
result := False ;
//释放资源
Windows.CloseHandle(h) ;
end ;
 
妙!妙!妙!妙!妙!妙!
 
procedure TFMForm.Properties1Click(Sender: TObject);
var
Attributes, NewAttributes: Word;
begin
with FileAttrForm do
begin
FileDirName.Caption := FileList.Items[FileList.ItemIndex];
{ set box caption }
PathName.Caption := FileList.Directory;
{ show directory name }
ChangeDate.Caption :=
DateTimeToStr(FileDateToDateTime(FileAge(FileList.FileName)));
Attributes := FileGetAttr(FileDirName.Caption);
{ read file attributes }
ReadOnly.Checked := (Attributes and faReadOnly) = faReadOnly;
Archive.Checked := (Attributes and faArchive) = faArchive;
System.Checked := (Attributes and faSysFile) = faSysFile;
Hidden.Checked := (Attributes and faHidden) = faHidden;
if ShowModal <> id_Cancel then { execute dialog box }
begin
NewAttributes := Attributes;
{ start with original attributes }
if ReadOnly.Checked then
NewAttributes := NewAttributes or faReadOnly
else
NewAttributes := NewAttributes andnot faReadOnly;
if Archive.Checked then
NewAttributes := NewAttributes or faArchive
else
NewAttributes := NewAttributes andnot faArchive;
if System.Checked then
NewAttributes := NewAttributes or faSysFile
else
NewAttributes := NewAttributes andnot faSysFile;
if Hidden.Checked then
NewAttributes := NewAttributes or faHidden
else
NewAttributes := NewAttributes andnot faHidden;
if NewAttributes <> Attributes then { if anything changed... }
FileSetAttr(FileDirName.Caption, NewAttributes);
{ ...write the new values }
end;
end;
end;
 
后退
顶部