Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

file-io

I am developing an application in C# (.NET), and am having trouble dealing with file locking.

  • My main application (A) needs read/write access to a certain file.
  • A separate application (B) needs read access to the same file.
  • I need to prevent the user from editing or deleting the file while my application (A) is running. The application (A) is long-running. The file must not be deleted while (A) is running, even when it is not actively being read from or written to.

I have full control of the source of (A) and (B), so I can modify either of them.

How can I stop a user from modifying/deleting a file while application (A) is running, while allowing application (A) to read/write, and application (B) to read?

like image 982
Matthew King Avatar asked Jul 19 '10 07:07

Matthew King


People also ask

How do I lock a file while editing?

Right-click a file (or click the ellipses (...)) to open the More Options menu. Click Lock. Choose a duration for the lock. If you choose unlimited, the file will be locked until you unlock it manually.

What is the purpose of locking a file?

File locking is a mechanism that restricts access to a computer file, or to a region of a file, by allowing only one user or process to modify or delete it at a specific time and to prevent reading of the file while it's being modified or deleted.

How do I lock file details?

Step 1. Right-click the file that you need to lock on a Windows computer, select Properties. Step 2. On the Properties window, go to General > Advanced > Check Encrypt contents to secure data.


2 Answers

Use FileShare.Read to only allow reads from other applications. You can lock the file by having a stream open while the application A runs. You need a NonClosingStreamWrapper to avoid disposing the stream when you dispose your StreamWriter (this happens automatically with using)

NonClosingStreamWrapper by Jon Skeet can be found from here

Example

When application starts use this to lock the file

FileStream fileStream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);

When writing to a file use

using (StreamWriter sr = new StreamWriter(new NonClosingStreamWrapper(fileStream)))
{
    // File writing as usual
}

When application ends use this to release the file

fileStream.Close();
like image 172
Cloudanger Avatar answered Oct 08 '22 04:10

Cloudanger


Most of the time, a locking of the file is not to prevent user deleting the file, but inform user running another instance of the application that the file is "in use" by another user. This is expecially useful if multiple users are opening r/w a file into a shared folder. In such scenario, instead of locking the file at filesystem level, would be much more easier to use a "lock file" generated when Appication (A) opens the file. Thus, any other application, would notice that a lock file exist (you can name it using the same filename but different extension), and also inside the locking file you can write who and when someone have aquired the lock. Application (B) can now respond to user... "The file appear to be under modification by user xxx from machine yyy, do you really want to load it ?"

Of course, the application must remove the lock file when the application file is no longer in use or when the application terminates. In the "unfortunate" case that a crash leave the lock on filesystem, user can just respond yes to the warning request, or can manually delete it to free the lock.

Hope this helps,

Paolo Marani

like image 38
user2991288 Avatar answered Oct 08 '22 06:10

user2991288