Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to read-lock a file?

I'm developing an application which checks for changes made to a file by a separate program (not written by me).

If a change is detected, it opens the file, reads the last line, then closes the file.

I'm using the following code to make sure my program doesn't try to lock the file, but only opens it in read mode:

FileStream fs =
    new FileStream(
        _scannerFilePath,
        FileMode.Open,
        FileAccess.Read,
        FileShare.ReadWrite);
StreamReader sr = new StreamReader(fs);
var str = sr.ReadToEnd();
sr.Close();
fs.Close();

Unfortunately, in spite of this, I'm still getting the following error whenever my program tries to read the file:

System.IO.IOException was unhandled
    Message="The process cannot access the file 'D:\\LSDATA\\IdText.txt' because it is being used by another process."
    Source="mscorlib"
    StackTrace:
        at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
        at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
        at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
        at LiquorSafe.Verification.Main.CheckLastScannedUser(String changedFileName)
        at LiquorSafe.Verification.Main.OnChanged(Object sender, FileSystemEventArgs e)
        at System.IO.FileSystemWatcher.OnChanged(FileSystemEventArgs e)
        at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32 action, String name)
        at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* overlappedPointer)
        at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
    InnerException: 

Is there any possible reason for this?

Could it be that the other program is somehow read-locking the file, and thus preventing me from even reading it?

like image 995
Jonathan Avatar asked Jul 15 '10 10:07

Jonathan


People also ask

How does a LOCK file work?

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.

What is write lock for a file?

An exclusive or write lock gives a process exclusive access for writing to the specified part of the file. While a write lock is in place, no other process can lock that part of the file. A shared or read lock prohibits any other process from requesting a write lock on the specified part of the file.

What does it mean to lock a file on Mac?

To protect documents from being accidentally changed or deleted, you can lock them. When a document is locked, you receive a warning when you move it to the Trash. When you empty the Trash, the locked document is deleted.


1 Answers

Yes, you can open a file in a so called Exclusive-Mode. This means nobody than yourself can read or write to that file.

If you take a look into the File.Open() function. There exists a parameter FileShare which can be set to None.

like image 112
Oliver Avatar answered Oct 11 '22 17:10

Oliver