mam*_*mcx 5 delphi cross-platform filesize compiler-warnings
我有这个rutine知道文件大小:
(基于http://delphi.about.com/od/delphitips2008/qt/filesize.htm)
function FileSize(fileName : String) : Int64;
var
sr : TSearchRec;
begin
if FindFirst(fileName, faAnyFile, sr ) = 0 then
{$IFDEF MSWINDOWS}
result := Int64(sr.FindData.nFileSizeHigh) shl Int64(32) + Int64(sr.FindData.nFileSizeLow)
{$ELSE}
result := sr.Size
{$ENDIF}
else
result := -1;
FindClose(sr) ;
end;
Run Code Online (Sandbox Code Playgroud)
但是,这会发出此警告:
[DCC Warning] Funciones.pas(61): W1002 Symbol 'FindData' is specific to a platform
Run Code Online (Sandbox Code Playgroud)
我想知道是否存在一种干净的跨平台方式来做到这一点.我检查了TFile类而没找到它......
在Delphi XE2中,该TSearchRec.Size成员已经是Int64(不确定哪个版本已更改)并且使用TSearchRec.FindDataWindows上字段的完整64位值填充,因此无需手动计算大小,例如:
{$IFDEF VER230}
{$DEFINE USE_TSEARCHREC_SIZE}
{$ELSE}
{$IFNDEF MSWINDOWS}
{$DEFINE USE_TSEARCHREC_SIZE}
{$ENDIF}
{$ENDIF}
function FileSize(fileName : String) : Int64;
var
sr : TSearchRec;
begin
if FindFirst(fileName, faAnyFile, sr ) = 0 then
begin
{$IFDEF USE_TSEARCHREC_SIZE}
Result := sr.Size;
{$ELSE}
Result := (Int64(sr.FindData.nFileSizeHigh) shl 32) + sr.FindData.nFileSizeLow;
{$ENDIF}
FindClose(sr);
end
else
Result := -1;
end;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2229 次 |
| 最近记录: |