FileForums

FileForums (https://fileforums.com/index.php)
-   Conversion Tutorials (https://fileforums.com/forumdisplay.php?f=55)
-   -   Check whether file is in use (https://fileforums.com/showthread.php?t=104847)

Cuttlas 19-12-2021 03:57

Check whether file is in use
 
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.

Code:

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.


All times are GMT -7. The time now is 03:21.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2022, vBulletin Solutions Inc.
Copyright 2000-2020, FileForums @ https://fileforums.com