轻松获取应用程序exe大小

Joh*_*ler 7 delphi size

在Delphi中有一种方法可以在一行或两行代码中获得当前应用程序的exe大小吗?

ska*_*adt 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;
Run Code Online (Sandbox Code Playgroud)


Rob*_*t K 5

它并不像你想要的那么小,但它不需要手柄.我在所有必须知道其大小的"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;