如何在delphi xe2上以跨平台方式获取文件大小

发布于 2025-01-01 17:09:14 字数 731 浏览 1 评论 0原文

我有这个程序来了解文件大小:(

基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

太阳男子 2025-01-08 17:09:14

在 Delphi XE2 中,TSearchRec.Size 成员已经是一个 Int64(不确定是哪个版本发生了变化),并用 TSearchRec.Size 中的完整 64 位值填充。 Windows 上的 code>TSearchRec.FindData 字段,因此无需手动计算大小,例如:

{$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; 

In Delphi XE2, the TSearchRec.Size member is already an Int64 (not sure which version that changed in) and is filled in with the full 64-bit value from the TSearchRec.FindData fields on Windows, so there is no need to calculate the size manually, eg:

{$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; 
拥醉 2025-01-08 17:09:14

您收到的警告是因为 TSearchRec 结构的 FindData 成员特定于 Windows 平台,但您无需担心它,因为在您的代码中您不是当您使用不同于 Windows 的平台时访问该成员。

// condition if you are on the Windows platform
{$IFDEF MSWINDOWS}
  // here you can access the FindData member because you are
  // on Windows
  Result := Int64(sr.FindData.nFileSizeHigh) shl Int64(32) + 
    Int64(sr.FindData.nFileSizeLow);
{$ELSE}
  // here you can't use FindData member and you would even 
  // get the compiler error because the FindData member is 
  // Windows specific and you are now on different platform
{$ENDIF}

The warning you are getting because the FindData member of the TSearchRec 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.

// condition if you are on the Windows platform
{$IFDEF MSWINDOWS}
  // here you can access the FindData member because you are
  // on Windows
  Result := Int64(sr.FindData.nFileSizeHigh) shl Int64(32) + 
    Int64(sr.FindData.nFileSizeLow);
{$ELSE}
  // here you can't use FindData member and you would even 
  // get the compiler error because the FindData member is 
  // Windows specific and you are now on different platform
{$ENDIF}
吾性傲以野 2025-01-08 17:09:14

因为您已经检查您正在 Windows 上运行,所以可以安全地在本地删除警告以仅保留编译器报告的“真实”警告:

  if FindFirst(fileName, faAnyFile, sr ) = 0 then
  {$IFDEF MSWINDOWS}
    {$WARN SYMBOL_PLATFORM OFF}
     result := Int64(sr.FindData.nFileSizeHigh) shl Int64(32) + Int64(sr.FindData.nFileSizeLow)
    {$WARN SYMBOL_PLATFORM ON}
  {$ELSE}

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:

  if FindFirst(fileName, faAnyFile, sr ) = 0 then
  {$IFDEF MSWINDOWS}
    {$WARN SYMBOL_PLATFORM OFF}
     result := Int64(sr.FindData.nFileSizeHigh) shl Int64(32) + Int64(sr.FindData.nFileSizeLow)
    {$WARN SYMBOL_PLATFORM ON}
  {$ELSE}
很快妥协 2025-01-08 17:09:14
TDirectory.GetLastWriteTime(path);
TDirectory.GetLastWriteTime(path);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文