c# – How do I know 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. whether it is being added. Is it possible to check if the file is writable? For example, I open a file through Adobe Reader, this file should not be copied. I rather need an answer to the question "How do I know if a file is open for modification by another program?"

Answer:

The most sober, in my opinion, way to understand whether a file is busy with 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 when there is no access to it. Attempts to implement the method to check whether the file is locked or not will give any special result. Because no one guarantees that in the interval between the call to the lock check method and the copying of the file, no other process has occupied it.

Scroll to Top