How to Lock a File with C#

How to lock a file with C#?

As per http://msdn.microsoft.com/en-us/library/system.io.fileshare(v=vs.71).aspx

FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.None);

How to lock/unlock a file across process?

Get help from mono mail list "http://mono.1490590.n4.nabble.com/File-Locking-td4663839.html"

below is the answer quote from "Edward Ned Harvey (mono)"

Kinda sorta. The underlying issue is that OSX, Linux, and Windows all
have different underlying file locking constructs, and then of course,
there's some variability about even which filesystem is being used.
I didn't thoroughly figure out all the answers for every OS or
filesystem, and I don't know under which situations this will be good
enough, but this is what I ended up using, works under the conditions
we needed it to work:

using (var foo = new FileStream(filePath, FileMode.Open,FileAccess.ReadWrite, FileShare.None)) { // must include Write access in order to lock file 
foo.Lock(0, 0); // 0,0 has special meaning to lock entire file regardless of length
}

For windows, simply specifying the FileAccess and FileShare is good
enough. For linux, at least ext4, files are concurrently readable
regardless of what you specify for FileAccess and FileShare. The
Lock() method does something of a soft-lock. It's not enforced by the
OS, but at least all the situations we tried, other client apps honor
the lock. Didn't look into it any deeper.

Should I lock a file before I read it in C#

No you don't need to lock the file before you read it. The only reason you would need to lock it is if you wanted to be sure that the file would not be altered while you are reading it.

Yes if you lock the file with that code then other users can get a lock error if they try and write to the file or read the file with read/write privleges. It is quite common with the programs i work on.

You should not lock files unless you actually need to.

if you dont want to lock the file then use the ReadWrite share enum.

   using(FileStream fileStream = new FileStream(
"myXmlFile.xml",
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite))

C# How to lock Access file & work with it?

The solution is indeed to create a separate lock file and lock this using a file stream.

(The answer is actually given by Sinatr, just for the sake of completeness I want to share it. Yet to find out is what MS Office does to its office files, as that is the exact behavior I'd like to have - but that is a different question.)

How to lock file

Simply open it exclusively:

using (FileStream fs = 
File.Open("MyFile.txt", FileMode.Open, FileAccess.Read, FileShare.None))
{
// use fs
}

Ref.

Update: In response to comment from poster: According to the online MSDN doco, File.Open is supported in .Net Compact Framework 1.0 and 2.0.

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

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();

C# how to lock a file from application start till application end

This works the first time:

XDocument xdoc = XDocument.Load(configurationFile);

The reason it throws an exception when you try to read it the second time is because you need to reset the stream position.

In your second form, reset the stream position before attempting to load the stream again:

configurationFile.Position = 0;               
XDocument xdoc2 = XDocument.Load(configurationFile);

How to lock a csv file with C#? V2

Try this:

using(FileStream fileStream = new FileStream(
saveNameTemporaryFull, FileMode.OpenOrCreate,
FileAccess.ReadWrite, FileShare.None))
{
StreamWriter arquivo2 = new StreamWriter(fileStream, true,sjisX2);
arquivo2.Write(tb_csvFull.Text);
arquivo2.Close();
}

Don't forget to close() fileStream;



Related Topics



Leave a reply



Submit