c# – How to find out if a file is available for opening and writing?

Question:

There is a folder on the network in which files are periodically created. There is a need to copy these files to another directory. You need to check if the file is available for copying, i.e. does he not subscribe? Is it possible to check if a file is writable? For example, I open a file through Adobe Reader, this file should not be copied. I need an answer rather to the question "How do I know if a file is open by another program for modification?"

Answer:

The most sober, in my opinion, way to understand whether the file is occupied by another process is to try to copy it and get a System.IO.IOException in case of an I / O error, which will just happen if there is no access to it. Attempts to implement a method to check whether a file is locked or not will not give a special result. Since no one guarantees that in the interval between calling the lock check method and copying the file, no other process has already taken it.

Scroll to Top