Fixer la taille du fichier

function FIND_FILE_SIZE(sFileToExamine:string;bInKBytes:boolean):string;
{for some reason both methods of finding file size returns a filesize that is
slightly larger than what Windows File Explorer reports, go figure}

var
FileHandle: THandle;
FileSize:LongWord;
d1:double;
i1:int64;

begin
//a- find file size
FileHandle := CreateFile(
PCHAR(sFileToExamine),
GENERIC_READ,
0, {exclusive}
nil, {security}
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
FileSize := GetFileSize(FileHandle,nil);
result := INTTOSTR(FileSize);
CloseHandle(FileHandle);
//a- optionally report back in Kbytes
if bInKbytes = true then
begin
if length(result)> 3 then
begin
Insert('.',result,length(result)-2);
d1 := STRTOFLOAT(result);
result := INTTOSTR(round(d1))+'KB';
end
else
result := '1KB';
end;
{******************************************************************************
Thanks to Advanced Delphi Systems here's another method which works just as
well returning the same results
*******************************************************************************

var
SearchRec: TSearchRec;
sgPath : String;
inRetval, I1 : Integer;

begin
sgPath := ExpandFileName(sFileToExamine);
Try
inRetval := FindFirst(ExpandFileName(sFileToExamine), faAnyFile, SearchRec);
If inRetval = 0 Then
I1 := SearchRec.Size
Else I1 := -1;
Finally
SysUtils.FindClose(SearchRec);
End;
result := INTTOSTR(I1);
end; }
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
Label1.Caption:=FIND_FILE_SIZE(Opendialog1.filename,true);
end;
end;
Ajouter un commentaire
Pseudo
Adresse email
Site internet (optionnel)
Votre commentaire
Fiche de l'article
Mise a jour22/11/2001
VisualisationVu 964 fois
PublicInternaute zz
CategorieDelphi - Trucs et astuces - Fichier
Auteur de l'article
SwissDelphiCenter SwissDelphiCenter
connecté le 02/11/2011
329 articles dans la section
Contacter l'auteur