Catch Delphi file size even though it is in use

Asked

Viewed 671 times

1

I need to get the file size from Delphi 7 but am getting I/O error because the file is being used. (The file is a . exe and is open)

I’ve tried the following codes:

function TamanhoDoArquivo(arquivo: string): LongInt;
var
  lArquivo: file of byte;
begin
  with TFileStream.Create(arquivo, fmOpenRead or fmShareExclusive) do
  try
    Result := Size;
  finally
    Free;
  end;
end;

And also:

function TamanhoDoArquivo(arquivo: string): LongInt;
var
  lArquivo: file of byte;
begin
  try
    AssignFile(lArquivo, arquivo);
    try
      reset(lArquivo);
      Result := FileSize(lArquivo);
    finally
      CloseFile(lArquivo)
    end;
  except
    on E:Exception do
      Showmessage('Erro');
end;
end;

Is there any way to get the size of . exe even if it is open? Thank you

1 answer

1


function FileSize(const aFilename: String): Int64;
var
    info: TWin32FileAttributeData;
begin
    result := -1;

    if NOT GetFileAttributesEx(PWideChar(aFileName), GetFileExInfoStandard, @info) then
        EXIT;

    result := Int64(info.nFileSizeLow) or Int64(info.nFileSizeHigh shl 32);
end;

Source: here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.