Trying to open flags for writing. If this fails, the file is "taken" by some other process.
FileStream fileStream = null; try { fileStream = new FileStream(@"c:\file.txt", FileMode.Open, FileAccess.Write); } catch (UnauthorizedAccessException e) { // The access requested is not permitted by the operating system // for the specified path, such as when access is Write or ReadWrite // and the file or directory is set for read-only access. } finally { if (fileStream != null) fileStream.Close (); }
PS Just found a very similar question, basically the same answer:
C #: Is there a way to check if a file is being used?
user151323
source share