c# - Lock file for writing/deleting while allowing any process to read -


I am developing an application in C # (.NET), and there is a problem in dealing with file locking.

  • My main application (A) needs to read / write a certain file.
  • A separate application (B) needs to be read in the same file.
  • I need to stop the user from editing or removing the adf, while my application is running (a) the application is running (a) for a long time (a) is running, even if it Should not be actively being read or written, the file should not be removed.

I have absolute control over (A) and (K) source B), so I can modify any of these.

How do I stop a user from modifying / removing a file, and the application to read (B) while allowing the application to read / write (A)?

Use only to FileShare. Read from other applications, you can lock the file by opening the stream when the application is running. When you settle your StreamWriter , you need to avoid removing it from NonClosingStreamWrapper (using automatically )

<

example

When the application starts using it to lock this file

pre-> filestream filestream = new filestream (file, file md.openOct, file accessed, readwrit, fileshare.read);

To use a file

 using  (streamerr sr = new streamer (new non-clausestream usage (filestream)) {/ / Normally writing files}  

When the application ends, its use to release the file

  fileStream.Close ();  

Comments