PDA

View Full Version : Check whether file is in use


Cuttlas
19-12-2021, 03:57
is there any way to check if a file is in use via inno setup?

Razor12911
19-12-2021, 20:42
You need to open the file with the intent to both read and writing to it, if this operation fails then it means the file is in use.

function IsFileInUse(Filename: String): Boolean;
begin
Result := False;
try
with TFileStream.Create(Filename,fmOpenReadWrite) do // this should fail
try
finally
Free;
end;
except
Result := True; // this handles the exception and tells you the file is in use
end;
end;

Cuttlas
20-12-2021, 21:26
what happens if the file is so big? does it take too long?

Razor12911
21-12-2021, 01:57
Size is irrelevant

Cesar82
21-12-2021, 08:17
@Razor12911, Just curiosity...
Is it necessary to use "try" "finally"?
It could not be used simply "do Free;"

Razor12911
21-12-2021, 23:50
You can write "do Free", it's the same thing only for this case as there is no code within the try..finally statement that would cause an exception.