Home » Delphi, Object Pascal, Referensi, Sample Apps, Tips & Tricks, Win32 API » Get Exact File Size Using Delphi

Get Exact File Size Using Delphi

Print Friendly



Do not get messed with Windows API CreateFile(), OpenFile() and GetFileSize() to only get a file’s actual size. Using VCL component API wrapper TFileStream, we can do this faster:

We Need This Reusable Function


function ObtainFileSize(const AFile: String): Int64;
begin
 with TFileStream.Create(AFile, fmOpenRead or fmShareDenyNone) do
 begin
 try
 Result := Size;
 finally
 Free;
 end;
 end;
end;

We Use It Like This


procedure TForm1.Button1Click(Sender: TObject);
var
 AFile: String;
 iSize: Int64;
begin
 AFile := Application.ExeName;
 iSize := ObtainFileSize(AFile);
 ShowMessage('Application EXE size: ' + IntToStr(iSize));d
end;

And Here’s Its Result


Incoming search terms:

 

Comments

comments

Powered by Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>