Получаем Размер Файла В Мб Или Гб

Mazahaka_lab

Принятый Кодер
Ofline
Вот функция , которую я часто использую. Позволяет узнать размер файла в мб или гб . Сверял с проводником , показывает один в один . Можно использовать как для маленьких файлов так и для больших .
Код:
function GetFileSize (namefile: string): Int64;
var InfoFile: TSearchRec;
    AttrFile: Integer;
    Error: Integer;
begin
 AttrFile := $0000003F; // Any file
 Error    := FindFirst(namefile, AttrFile, InfoFile);
 if Error <> 0 then result := -1
  else begin
   Result := InfoFile.FindData.nFileSizeHigh;
   Result := Result shl 32;
   Result := Result or InfoFile.FindData.nFileSizeLow
  end;
 FindClose(InfoFile)
end;

function StrFormatByteSizeW(ASize: UInt64; szBuf: PWideChar; uiBufSize: UINT): PWideChar; stdcall; external 'shlwapi.dll';
function FmtFormatSize(const ASize: UInt64): String;
var
Buf: WideString;
begin
SetLength(Buf, 1024);
if StrFormatByteSizeW(ASize, PWideChar(Buf), Length(Buf)) <> nil then
Result := PWideChar(Buf)
else
Result := UIntToStr(ASize);
end;

Пример использования:
procedure TForm1.Button1Click(Sender: TObject);
begin
caption:='Размер файла - '+FmtFormatSize( GetFileSize('C:\DaRT10.iso'));
end;
 
Назад
Сверху Снизу