使用Delphi 7开发我们的主要应用程序.我们正在导入数据文件.以字节为单位的文件大小超过了vcl和我的代码用来保存它的整数变量的大小......所以它变为负数并且采取空文件操作......
我们当前检查文件大小的代码(从而决定是否为空)是:
function getfilesize(vfilename: string): integer;
var
SearchRec: TSearchRec;
begin
try
result:= -1;
if FindFirst(vfilename, faAnyFile, SearchRec) = 0 then
result:= SearchRec.Size;
FindClose(SearchRec);
except
on e: exception do
raise exception.create('Error: functions\getfilesize - Unable to analyze file Attributes to determine filesize. '#13#10+e.message);
end;
Run Code Online (Sandbox Code Playgroud)
多年来,这种情况来回变化,但在过去的5年里,这种方法运作良好.
searchrec.size是一个INTEGER,所以只更改我们的返回类型是不够的.存储中还有许多其他因素,与我们的代码和我们使用的数据库字段有关.
问:确定文件大小以字节为单位的其他D7方法对我们有用,它们使用更大的数据类型?
问:你知道getFilesize在更大整数中的任何其他替换函数吗?
GetFileAttributesEx()是最方便的Windows API调用.它是最快的,不同于GetFileSize()不需要您获取文件句柄.
像这样包装:
function FileSize(const FileName: string): Int64; overload;
var
AttributeData: TWin32FileAttributeData;
begin
if not GetFileAttributesEx(PChar(FileName), GetFileExInfoStandard, @AttributeData) then
RaiseLastOSError;
Int64Rec(Result).Lo := AttributeData.nFileSizeLow;
Int64Rec(Result).Hi := AttributeData.nFileSizeHigh;
end;
Run Code Online (Sandbox Code Playgroud)
如果您碰巧有文件句柄,那么GetFileSizeEx()最好:
function GetFileSizeEx(hFile: THandle; var FileSize: Int64): BOOL; stdcall; external kernel32;
function FileSize(hFile: THandle): Int64; overload;
begin
if not GetFileSizeEx(hFile, Result) then
RaiseLastOSError;
end;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2067 次 |
| 最近记录: |