Is there a way I can check if a file is in use or is not opened by other process without just trying to open it and catching an exception? Is there no service method to test such a thing?
Even if there was, it wouldn't do you much good since you would still have to catch the exception in order to handle the race condition where the file became unavailable in between your initial check and your actual attempt to open/access it.
I can't think of any compelling advantage to a preliminary defensive check. It just results in unnecessary code duplication.
If there were such a IsFileAccessible
function, it would probably be implemented as a giant try/catch block that attempted to open the file, caught failures, and returned the result.
Can I test if a file can be opened without attempting to open it?
The .net framework, just like the Windows API beneath, provides no such functionality. If you wish to know whether or not a file can be opened you are expected to attempt to open it and check for failure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With