function GetFileSize(const FileName: string): Int64;
var
SR: TSearchRec;
begin
Result := -1;
if FindFirst(FileName, faAnyFile and (not faDirectory), SR) = 0 then
try
Result := SR.Size;
finally
FindClose(SR);
end;
end;
该函数用于获取指定文件的大小,对比万一的函数,主要有两点不同:
1. 排除目录:faAnyFile and (not faDirectory)
如果只使用faAnyFile,文件名是个目录的话,照样可以获得一个正确结果,只是结果为零。
2. 释放占用的内存资源:FindClose(SR)
Note: FindFirst allocates resources (memory) which must be released by calling FindClose. (官方说明)