在Delphi 2010或更高版本中获取文件大小? [英] Getting size of a file in Delphi 2010 or later?

查看:273
本文介绍了在Delphi 2010或更高版本中获取文件大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Delphi 2010在 IOUtils.pas (我特别喜欢UTC版本的日期相关函数)中有一组不错的新文件访问功能。到目前为止,我想念的是像

Delphi 2010 has a nice set of new file access functions in IOUtils.pas (I especially like the UTC versions of the date-related functions). What I miss so far is something like

TFile.GetSize (const Path : String)

什么是Delphi 2010 - 获取文件大小的方式?我必须返回并使用FindFirst访问 TSearchRec.FindData

What is the Delphi 2010-way to get the size of a file? Do I have to go back and use FindFirst to access TSearchRec.FindData?

谢谢。

推荐答案

我不知道是否有Delphi 2010的方式,但是有一个不涉及FindFirst和所有爵士乐的Windows方式

I'm not sure if there's a "Delphi 2010" way, but there is a Windows way that doesn't involve FindFirst and all that jazz.

我把这个例程的Delphi转换扔在一起(在这个过程中修改它来处理> 4GB大小的文件,如果你需要的话)。

I threw together this Delphi conversion of that routine (and in the process modified it to handle > 4GB size files, should you need that).

  function FileSize(const aFilename: String): Int64;
  var
    info: TWin32FileAttributeData;
  begin
    result := -1;

    if NOT GetFileAttributesEx(PWideChar(aFileName), GetFileExInfoStandard, @info) then
      EXIT;

    result := Int64(info.nFileSizeLow) or Int64(info.nFileSizeHigh shl 32);
  end;

你实际上可以使用 GetFileSize(),但这需要一个文件HANDLE ,而不仅仅是文件名,并且类似于 GetCompressedFileSize()建议,这需要调用两个变量。 GetFileSize() GetCompressedFileSize()重载它们的返回值,所以测试成功并确保有效的结果只是一点点尴尬。

You could actually just use GetFileSize() but this requires a file HANDLE, not just a file name, and similar to the GetCompressedFileSize() suggestion, this requires two variables to call. Both GetFileSize() and GetCompressedFileSize() overload their return value, so testing for success and ensuring a valid result is just that little bit more awkward.

GetFileSize Ex ()避免了处理> 4GB文件大小和检测有效结果的严谨性,但也需要一个文件HANDLE比起一个名字,(在Delphi 2009至少我没有检查过2010年)没有在VCL任何地方为你声明,你必须提供你自己的导入声明。

GetFileSizeEx() avoids the nitty gritty of handling > 4GB file sizes and detecting valid results, but also requires a file HANDLE, rather than a name, and (as of Delphi 2009 at least, I haven't checked 2010) isn't declared for you in the VCL anywhere, you would have to provide your own import declaration.

这篇关于在Delphi 2010或更高版本中获取文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆