Does anyone know how to check if a file is already open?
Open the file with ReadWrite file access. Catch the IOException.

If there’s no exception, it’s not already open. If there’s an exception, it’s already open. (This works because Windows won’t give you the writable file handle if it’s open elsewhere.)
Stack Overflow from 8 years ago.
Stack Overflow from 10 years ago.
Stack Overflow from 11 years ago.
There is not a good way of doing this without using exception handling for flow control. Yes, that sucks, but it’s all there is in this case. Blame Microsoft.
C# devs
null reference exceptions

source