轻松获取应用程序exe大小

发布于 2024-07-07 17:44:10 字数 45 浏览 8 评论 0原文

Delphi中有没有一种方法可以通过一两行代码获取当前应用程序的exe大小?

Is there a way in Delphi to get the currect application's exe size in one or two lines of code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(9

咆哮 2024-07-14 17:44:10

只是为了笑……你也可以用流来做到这一点,只需略多于 2 行代码。 通常,应用程序文件名(包括路径)也存储到 Paramstr(0) 中。

var
  fs : tFilestream;
begin
  fs := tFilestream.create(paramstr(0),fmOpenRead or fmShareDenyNone);
  try
    result := fs.size;
  finally
    fs.free;
  end;
end;

Just for grins...you can also do this with streams Just slightly more than 2 lines of code. Generally the application filename including path is also stored into Paramstr(0).

var
  fs : tFilestream;
begin
  fs := tFilestream.create(paramstr(0),fmOpenRead or fmShareDenyNone);
  try
    result := fs.size;
  finally
    fs.free;
  end;
end;
半步萧音过轻尘 2024-07-14 17:44:10

它没有你想要的那么小,但它不需要把手。 我在所有必须知道其大小的“SFX”存档器和程序中使用它。 IIRC 它需要 Windows 设备。

function GetExeSize: cardinal;
var
  p: pchar;
  i, NumSections: integer;
const
  IMAGE_PE_SIGNATURE  = $00004550;
begin
  result := 0;
  p := pointer(hinstance);
  inc(p, PImageDosHeader(p)._lfanew + sizeof(dword));
  NumSections := PImageFileHeader(p).NumberOfSections;
  inc(p,sizeof(TImageFileHeader)+ sizeof(TImageOptionalHeader));
  for i := 1 to NumSections do
  begin
    with PImageSectionHeader(p)^ do
      if PointerToRawData+SizeOfRawData > result then
        result := PointerToRawData+SizeOfRawData;
    inc(p, sizeof(TImageSectionHeader));
  end;
end;

It's not as small as you want, but it needs no handles. I use this in all my "SFX" archivers and programs that must know their size. IIRC it requires the Windows unit.

function GetExeSize: cardinal;
var
  p: pchar;
  i, NumSections: integer;
const
  IMAGE_PE_SIGNATURE  = $00004550;
begin
  result := 0;
  p := pointer(hinstance);
  inc(p, PImageDosHeader(p)._lfanew + sizeof(dword));
  NumSections := PImageFileHeader(p).NumberOfSections;
  inc(p,sizeof(TImageFileHeader)+ sizeof(TImageOptionalHeader));
  for i := 1 to NumSections do
  begin
    with PImageSectionHeader(p)^ do
      if PointerToRawData+SizeOfRawData > result then
        result := PointerToRawData+SizeOfRawData;
    inc(p, sizeof(TImageSectionHeader));
  end;
end;
む无字情书 2024-07-14 17:44:10

为了将来的兼容性,您应该尽可能选择不需要指针或 Windows API 函数的实现。 skamradt 提供的基于 TFileStream 的解决方案对我来说看起来不错。

但是...您不必太担心该例程是 1 行代码还是 10 行代码,因为无论如何您都会将其封装在一个函数中,该函数以文件名作为参数并返回 Int64,并将其放入您的个人可重用代码库。 然后你可以像这样调用它:

GetMyFileSize(Application.ExeName);

For the sake of future compatibility, you should choose an implementation that does not require pointers or Windows API functions when possible. The TFileStream based solution provided by skamradt looks good to me.

But... You shouldn't worry too much whether the routine is 1 or 10 lines of code, because you're going to encapsulate it anyway in a function that takes a filename as a parameter and returns an Int64, and put it in your personal library of reusable code. Then you can call it like so:

GetMyFileSize(Application.ExeName);

2024-07-14 17:44:10

你可以试试这个:

  if FindFirst(ExpandFileName(Application.exename), faAnyFile, SearchRec) = 0 then
    MessageDlg(Format('Tamaño: <%d>',[SearchRec.Size]), mtInformation, [mbOK], 0);
  FindClose(SearchRec);

===============
内夫塔利

You can try this:

  if FindFirst(ExpandFileName(Application.exename), faAnyFile, SearchRec) = 0 then
    MessageDlg(Format('Tamaño: <%d>',[SearchRec.Size]), mtInformation, [mbOK], 0);
  FindClose(SearchRec);

===============
Neftalí

吲‖鸣 2024-07-14 17:44:10

流也可以在没有 TFileStream 变量的情况下使用:

with TFilestream.create(paramstr(0), fmOpenRead or fmShareDenyNone) do 
  aFileSize := Size;
  Free;
end;

丑陋,是的。

我更喜欢使用 DSiWin32 中的 DSiFileSize。 它在内部使用 CreateFile:

function DSiFileSize(const fileName: string): int64;
var
  fHandle: DWORD;
begin
  fHandle := CreateFile(PChar(fileName), 0, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if fHandle = INVALID_HANDLE_VALUE then
    Result := -1
  else try
    Int64Rec(Result).Lo := GetFileSize(fHandle, @Int64Rec(Result).Hi);
  finally CloseHandle(fHandle); end;
end; { DSiFileSize }

Streams can also be used without a TFileStream variable:

with TFilestream.create(paramstr(0), fmOpenRead or fmShareDenyNone) do 
  aFileSize := Size;
  Free;
end;

Ugly, yes.

I prefer using DSiFileSize from DSiWin32. It uses CreateFile internally:

function DSiFileSize(const fileName: string): int64;
var
  fHandle: DWORD;
begin
  fHandle := CreateFile(PChar(fileName), 0, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if fHandle = INVALID_HANDLE_VALUE then
    Result := -1
  else try
    Int64Rec(Result).Lo := GetFileSize(fHandle, @Int64Rec(Result).Hi);
  finally CloseHandle(fHandle); end;
end; { DSiFileSize }
假装爱人 2024-07-14 17:44:10

不幸的是,如果不使用某些库,仅用一两行代码就不可能做到这一点。

最简单的部分是获取应用程序的 exe 文件。 您可以在 Application.ExeName 中找到它。

一般来说,检索文件大小有多种可能性:

  1. 打开文件并读取流的大小。 这可以使用“旧”Delphi 函数 FileOpenFileSize 或使用 TFileStream(使用 size属性)或使用 Win32 API 函数 CreateFileGetFileSize 函数。 (取决于平台!)确保以只读访问权限打开文件。
  2. 在纯 Win32 环境中,您可以使用 FindFirst 来获取文件大小。 您可以从TSearchRec.FindData.nFileSizeLow读取它。 如果您想为大于 2 GB 的文件做好准备(您应该这样做),您还必须使用 nFileSizeHigh 部分。
  3. 在 Delphi.NET 中,您可以使用 System.IO.FileInfo,如下所示: FileInfo.Create(filename).Length (单行)
  4. 在 Linux 中,您可以使用lstat64 函数(Unit Libc)并从 TStatBuf64.st_size 获取大小。 (如果不计算变量声明,则为两行)

JCL 库 中,您可以找到许多有用的函数,包括一个返回给定文件名的文件大小的简单函数。 (它使用适合给定平台的方法)

Unfortunatly it is not possible to do that with only one or two lines of code without using some library.

The easy part is getting the application's exe file. You can find it in Application.ExeName

In general there are several possibilities for retrieving the file size:

  1. Open the file and read the size of the stream. This can be accomplished using the 'old' Delphi functions FileOpen and FileSize, or with TFileStream (use the size property) or with Win32 API functions CreateFile and GetFileSize function. (Platform dependend!) Make sure you open the file with read-only access.
  2. In a pure Win32 envinronment you can use FindFirst to get the file size. You can read it from TSearchRec.FindData.nFileSizeLow. If you want to be prepared for files larger than 2 GB (you should be) you have to use also the nFileSizeHigh part.
  3. In Delphi.NET you can use the System.IO.FileInfo, like this: FileInfo.Create(filename).Length (one-liner)
  4. In Linux you can use the lstat64 function (Unit Libc) and get the size from TStatBuf64.st_size. (two-liner if you don't count the variable declaration)

In the JCL library you can find many useful functions, including a simple function which returns the file size of a given file name. (It uses a method which suits the given platform)

汐鸠 2024-07-14 17:44:10
uses IdGlobalProtocols;

var
  ExeSize: Int64;
begin
  ExeSize := FileSizeByName(ParamStr(0)); 
  // or
  ExeSize := FileSizeByName(Application.ExeName);
end;
uses IdGlobalProtocols;

var
  ExeSize: Int64;
begin
  ExeSize := FileSizeByName(ParamStr(0)); 
  // or
  ExeSize := FileSizeByName(Application.ExeName);
end;
酸甜透明夹心 2024-07-14 17:44:10

我想修改 skamradt 提供的代码,使其成为您要求的两行代码;-)

  with tFilestream.create(paramstr(0),fmOpenRead or fmShareDenyNone) do
    ShowMessage(IntToStr(size));

但我更愿意使用 skamradt 编写的代码,因为它更安全

I would like to modify the code provided by skamradt, to make it two lines of code as you requested ;-)

  with tFilestream.create(paramstr(0),fmOpenRead or fmShareDenyNone) do
    ShowMessage(IntToStr(size));

but I would prefer to use the code as skamradt wrote, because it's more safe

多情癖 2024-07-14 17:44:10

我能做的最短的。 请注意,.Size 以字节为单位,因此对于千字节,请除以 1024。

procedure TForm1.Button1Click(Sender: TObject);
begin
  with TFileStream.Create(Application.ExeName,fmShareDenyNone) do
    ShowMessage(FloatToStr(Size/1024));
end;

查看 这个链接。

Shortest I could do. Note that the .Size is in bytes, so for kilobytes, divide by 1024.

procedure TForm1.Button1Click(Sender: TObject);
begin
  with TFileStream.Create(Application.ExeName,fmShareDenyNone) do
    ShowMessage(FloatToStr(Size/1024));
end;

Check out this link.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文