Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.IO.FileStream FileAccess vs FileShare

Tags:

c#

filestream

I've searched all over but can't find an answer to this question. I understand that FileAccess deals with file access on the machine and FileShare deals with the share, but I can't find an explanation of how exactly it comes together and how they impact one another.

For example, if I have

using ( FileStream fs = new FileStream( pathName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) ) 

does that mean that users on the machine can only read the file whilst users remotely accessing the folder can read and write to the file? Additionally, what would the impact be in using

using ( FileStream fs = new FileStream( pathName, FileMode.Open, FileAccess.Read ) ) 

Where I haven't specified the FileShare?

like image 357
Storm Avatar asked Aug 02 '14 17:08

Storm


People also ask

What is FileMode C#?

A FileMode parameter is specified in many of the constructors for FileStream, IsolatedStorageFileStream, and in the Open methods of File and FileInfo to control how a file is opened. FileMode parameters control whether a file is overwritten, created, opened, or some combination thereof.

How do I access a file in C#?

Open(String, FileMode, FileAccess) Method in C# with Examples. File. Open(String, FileMode, FileAccess) is an inbuilt File class method that is used to open a FileStream on the specified path with the specified mode and access with no sharing.

What is System IO FileStream?

The FileStream is a class used for reading and writing files in C#. It is part of the System.IO namespace. To manipulate files using FileStream, you need to create an object of FileStream class. This object has four parameters; the Name of the File, FileMode, FileAccess, and FileShare.

What is file share enumeration?

A typical use of this enumeration is to define whether two processes can simultaneously read from the same file. For example, if a file is opened and Read is specified, other users can open the file for reading but not for writing.


2 Answers

FileAccess says what you are going to do with the file. Pretty easy to understand, you'll know you are going to read or write.

FileShare is the much trickier one since it requires you to step into the shoes of another programmer. It determines what another process can do if it also has the file opened. Two processes accessing a file can be very troublesome, you'll need to reason through the possible failure modes. The value you pick is strongly correlated to the type of the file and the access you want. Breaking it down by what you are going to do:

FileAccess.Read

There is never any trouble if another process also reads from the file. So FileShare.Read is the default choice.

You may need FileShare.ReadWrite if another process has already opened the file for writing. It already gained write access so you can never open the file yourself with just FileShare.Read, you can't deny writing since that other process was first, you'll be denied access instead. This generally only comes to a good on a text file, the kind where you can be sure that the other process is only ever appending text to the end of the file. A log file is the very common scenario. Still possibly tricky, it matters exactly when that process flushes changes to the file. You might observe partially written lines of text, beware of this.

FileAccess.Write

You cannot use FileShare.Write or FileShare.ReadWrite. Since that would allow two processes writing to the file at the same time, the file content will be a jumble of output from both programs. A possible workaround is these processes arbitrating access to the file, ensuring only one of them can ever access the file at the same time. Normally implemented by a named mutex.

You can use FileShare.Read if it is text file, same scenario I described above with the log file. The default choice otherwise should be FileShare.None

FileAcces.ReadWrite

Not that common, only used if you write binary data and use Seek(). There is no hope that any other process could ever read the file correctly while you are doing this, assuming they don't arbitrate access themselves, you must use FileShare.None.

like image 176
Hans Passant Avatar answered Sep 25 '22 05:09

Hans Passant


The FileShare has nothing to do with drives shared over a network - it indicates how other processes can access the file.

If the first process opens the file with FileShare.Read, other processes can open the file with FileAccess.Read, but another process cannot open the file with FileAccess.Write in that case.

like image 26
C.Evenhuis Avatar answered Sep 22 '22 05:09

C.Evenhuis