[经验分享帖]关于FileSize无法正确取得文件大小超过2G的的问题(0)

  • 主题发起人 主题发起人 steve0531
  • 开始时间 开始时间
S

steve0531

Unregistered / Unconfirmed
GUEST, unregistred user!
一个维护很久的系统,突然出现问题,调试得知问题出在FileSize上。由于FileSize返回值是Integer,那么文件字节超出Integer范围时,返回的文件大小则为负值了。经过2个小时的解决,终于使用如下方案解决。function FileSizeEX(const FileName: string): Int64;var FileInfo: TWin32FindData; FileHandle : THandle;begin Result := -1; FileHandle := FindFirstFile(PChar(FileName), FileInfo); if FileHandle <> INVALID_HANDLE_VALUE then begin Windows.FindClose(FileHandle); Int64Rec(Result).Lo := FileInfo.nFileSizeLow; Int64Rec(Result).Hi := FileInfo.nFileSizeHigh; end;end;看来以后都要使用这个FileSizeEx了。在这里提醒使用FileSize的朋友,抓紧换掉吧,免得以后出了问题莫名其妙。Indy还有个FileSizeByName函数,但它不能在文件已经被操作的状态下执行,不适合我的需求,而且还要引用IdGlobalProtocols单元,VCL库中还有个GetFileSize,个人感觉都不如这个自定义的FileSizeEx函数方便。由于系统使用的是自定义的数据库,还有个索引文件,这个索引文件记录了各个偏移值,原来用的是Integer,唉,要对这个库的读写系统改版了,换成Int64。今天晚上再弄吧。
 
收藏了~~
 
所以我一直强调,学编程一定要懂英文.The GetFileSize function retrieves the size, in bytes, of the specified file. DWORD GetFileSize( HANDLE hFile, // handle of file to get size of LPDWORD lpFileSizeHigh // address of high-order word for file size ); ParametershFileSpecifies an open handle of the file whose size is being returned. The handle must have been created with either GENERIC_READ or GENERIC_WRITE access to the file. lpFileSizeHighPoints to the variable where the high-order word of the file size is returned. This parameter can be NULL if the application does not require the high-order word. Return ValuesIf the function succeeds, the return value is the low-order doubleword of the file size, and, if lpFileSizeHigh is non-NULL, the function puts the high-order doubleword of the file size into the variable pointed to by that parameter. If the function fails and lpFileSizeHigh is NULL, the return value is 0xFFFFFFFF. To get extended error information, call GetLastError. If the function fails and lpFileSizeHigh is non-NULL, the return value is 0xFFFFFFFF and GetLastError will return a value other than NO_ERROR.
 
不是不想学啊 。
 
接受答案了.
 
后退
顶部