如何在delphi xe2上以跨平台方式获取文件大小
我有这个程序来了解文件大小:(
基于 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;
但是,这给出了这个警告:
[DCC Warning] Funciones.pas(61): W1002 Symbol 'FindData' is specific to a platform
我想知道是否存在一种干净的跨平台方法来执行此操作。我检查 TFile 类但没有找到它......
I have this rutine to know the filesize:
(Based on 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;
However, this give this warning:
[DCC Warning] Funciones.pas(61): W1002 Symbol 'FindData' is specific to a platform
I wonder if exist a clean cross-platform way to do this. I check TFile class and not found it...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Delphi XE2 中,
TSearchRec.Size
成员已经是一个Int64
(不确定是哪个版本发生了变化),并用TSearchRec.Size
中的完整 64 位值填充。 Windows 上的 code>TSearchRec.FindData 字段,因此无需手动计算大小,例如:In Delphi XE2, the
TSearchRec.Size
member is already anInt64
(not sure which version that changed in) and is filled in with the full 64-bit value from theTSearchRec.FindData
fields on Windows, so there is no need to calculate the size manually, eg:您收到的警告是因为
TSearchRec
结构的FindData
成员特定于 Windows 平台,但您无需担心它,因为在您的代码中您不是当您使用不同于 Windows 的平台时访问该成员。The warning you are getting because the
FindData
member of theTSearchRec
structure is specific to Windows platform, but you don't need to worry about it because in your code you are not accessing that member when you are on the platform different from Windows.因为您已经检查您正在 Windows 上运行,所以可以安全地在本地删除警告以仅保留编译器报告的“真实”警告:
Because you already check you are running on Windows, it is safe to remove locally the Warning to keep only "real" warnings reported by the compiler: