Is there an easy way to determine if a file is open in any process on Windows?
For example, I control the directory, and if the files are placed in the directory, I want to perform some actions on these files.
I do not want to perform these actions if the files are still being copied to the directory or if the contents of these files are still being updated.
So what happens if a file name is given, I want to implement a function, for example, the IsFileOpenedAnywhereElseInAnyProcess(const PathName: string): Boolean function, which returns either true or false .
One of the ways I can think of is to rename the file, and if the renaming succeeds, no other processes have opened the file of interest to me, for example:
function IsFileOpenedAnywhereElseInAnyProcess(const PathName: string): Boolean; begin Result := not (MoveFileEx(PathName, PathName+'.BLAH', 0) and MoveFileEx(PathName+'.BLAH', PathName, 0)); end;
Logic, if I can rename a file (to another extension), then rename it back, it does not open by any other process (during verification).
Thanks.
source share