我来灌一桶!
用API函数GetFileInformationByHandle,函数声明如下
GetFileInformationByHandle(hFile: THandle;
var lpFileInformation: TbyHandleFileInformation): BOOL;
用来获取文件或文件夹的各种信息!关于函数的参数含义可以到Delphi的 MS SDK 帮助
里找,下面特意找了一个例子,比较长,复制下来吧,自己好好研究吧!
procedure TForm1.FileListBox1Change(Sender: TObject);
var
Security: TSecurityAttributes; // security attributes for the file
hFile: Integer; // holds the file handle
FileInfo: TByHandleFileInformation; // holds the file information
Intermediate: TFileTime; // holds a file time
SystemTime: TSystemTime; // holds the converted file time
FileType: DWORD; // holds the file type
AMPM: string; // morning/evening indicator
begin
{clear the status bar}
StatusBar1.SimpleText:= '';
{initialize the security information}
Security.nLength:=SizeOf(TSecurityAttributes);
Security.bInheritHandle:=FALSE;
{open the selected file for reading}
hFile:=CreateFile(PChar(FileListBox1.FileName), GENERIC_READ, 0, @Security,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile <> INVALID_HANDLE_VALUE) then
begin
{retrieve the file information}
GetFileInformationByHandle(hFile,FileInfo);
{display the selected file's attributes}
checkBox1.Checked := BOOLEAN(FileInfo.dwFileAttributes and
FILE_ATTRIBUTE_ARCHIVE);
CheckBox2.Checked := BOOLEAN(FileInfo.dwFileAttributes and
FILE_ATTRIBUTE_DIRECTORY);
CheckBox3.Checked := BOOLEAN(FileInfo.dwFileAttributes and
FILE_ATTRIBUTE_HIDDEN);
CheckBox4.Checked := BOOLEAN(FileInfo.dwFileAttributes and
FILE_ATTRIBUTE_OFFLINE);
CheckBox5.Checked := BOOLEAN(FileInfo.dwFileAttributes and
FILE_ATTRIBUTE_READONLY);
CheckBox6.Checked := BOOLEAN(FileInfo.dwFileAttributes and
FILE_ATTRIBUTE_SYSTEM);
CheckBox7.Checked := BOOLEAN(FileInfo.dwFileAttributes and
FILE_ATTRIBUTE_NORMAL);
CheckBox8.Checked := BOOLEAN(FileInfo.dwFileAttributes and
FILE_ATTRIBUTE_TEMPORARY);
{display the file name}
Label1.Caption := ExtractFileName(FileListBox1.FileName);
{we must first convert the file time into the local file time,
and then convert this into the system time to get the correct
modification time}
FileTimeToLocalFileTime(FileInfo.ftLastWriteTime, Intermediate);
FileTimeToSystemTime(Intermediate, SystemTime);
{indicate morning or evening, and modify the time so we are
not displaying military standard}
if SystemTime.wHour>11 then AMPM := ' PM' else AMPM := ' AM';
if SystemTime.wHour>12 then SystemTime.wHour := SystemTime.wHour-12;
{display the time}
Label2.Caption := IntToStr(SystemTime.wMonth)+'/'+
IntToStr(SystemTime.wDay)+
'/'+IntToStr(SystemTime.wYear)+' '+
IntToStr(SystemTime.wHour)+':'+
IntToStr(SystemTime.wMinute)+':'+
IntToStr(SystemTime.wSecond)+AMPM;
{display the volume serial number}
Label8.Caption:=IntToStr(FileInfo.dwVolumeSerialNumber);
{display the file size}
Label4.Caption:=IntToStr(GetFileSize(hFile, nil))+ ' bytes';
{display the file type}
FileType:=GetFileType(hFile);
case (FileType) of
FILE_TYPE_UNKNOWN: Label6.Caption:='File is of unknown type';
FILE_TYPE_DISK : Label6.Caption:='File is disk based';
FILE_TYPE_CHAR : Label6.Caption:='File is a character file';
FILE_TYPE_PIPE : Label6.Caption:='File is a named or anonymous pipe';
end;
{we are through examining the file, so close the handle}
CloseHandle(hFile);
end
else
{if the file could not be opened, indicate that it is in use}
StatusBar1.SimpleText:= 'File is in use';
end;