怎样读出目录的创建时间?(50分)

  • 主题发起人 主题发起人 xiaohui
  • 开始时间 开始时间
var
data:WIN32_FILE_ATTRIBUTE_DATA;
systime:SYSTEMTIME;
local:FILETIME;
begin
if(not GetFileAttributesEx('c:/books',GetFileExInfoStandard,@data))then
RaiseLastWin32Error();

FileTimeToLocalFileTime(data.ftCreationTime,local); //要转换为本地时间,否则为标准时间
FileTimeToSystemTime(local,systime);

with systime do
ShowMessage(Format('%d-%d-%d %d:%d:%d',[wYear,wMonth,wDay,wHour,wMinute,wSecond]));

end;
 
我也来凑热闹!
下列代码改写于Delphi的FileAge函数.


procedure TForm1.Button1Click(Sender: TObject);
var
Handle: THandle;
FindData: TWin32FindData;
LocalFileTime: TFileTime;
SystemTime:TSystemTime;
begin
Handle := FindFirstFile(PChar('c:/temp'), FindData);
if Handle <> INVALID_HANDLE_VALUE then
begin
Windows.FindClose(Handle);
FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);
FileTimeToSystemTime(LocalFileTime,SystemTime);
ShowMessage(DateTimeToStr(SystemTimeToDateTime(SystemTime)));
end;
end;
 
同意沈前卫同志的意见,
FindFirstFile/FindFirst是比较常用的查找文件并取文件一些小资料的函数,
取图标可用SHGetFileInfo。

不过提醒沈同志,您忘记关闭查找了:FindClose,这可不妙。
 
To 3h:
其实我早注意到了.不过见Delphi的FileAge原码:

function FileAge(const FileName: string): Integer;
var
Handle: THandle;
FindData: TWin32FindData;
LocalFileTime: TFileTime;
begin
Handle := FindFirstFile(PChar(FileName), FindData);
if Handle <> INVALID_HANDLE_VALUE then
begin
Windows.FindClose(Handle);
if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
begin
FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);
if FileTimeToDosDateTime(LocalFileTime, LongRec(Result).Hi,
LongRec(Result).Lo) then Exit;
end;
end;
Result := -1;
end;
 
Win32.HLP中有关FindFirstFile:

Return Values

If the function succeeds, the return value is a search handle used in a subsequent call to FindNextFile or FindClose.

If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.
 
对不起,我的眼被“windows”蒙着了。:)
(没脸见人)
 
后退
顶部