Get file size in Delphi even while in use

1

I need to get the size of a file through delphi 7 however I am getting an I / O error because the file is being used. (The file is an .exe and is open)

I have 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 a way to get the size of .exe even though it is open? Thanks

    
asked by anonymous 20.06.2018 / 19:49

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

    
20.06.2018 / 20:14