Mazahaka_lab
Принятый Кодер
- Регистрация
- 25 Янв 2017
- Сообщения
- 49
- Баллы
- 50
Вот функция , которую я часто использую. Позволяет узнать размер файла в мб или гб . Сверял с проводником , показывает один в один . Можно использовать как для маленьких файлов так и для больших .
Код:
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;