System.IO.FileStream FileAccess vs FileShare
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?
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:
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.
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
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.
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.
FileShare has nothing to do with Windows file shares. It talks about sharing files on the same machine only. It specifies what other file handles coming after you are maximally able to do. Also, existing handles are compared to it to decide whether you can open the file or not.
FileAccess specifies your access rights.
Our Awesome Tools
- Check your IP Address precisely
- Online JSON Formatter with Syntax Highlight
- Online CSS Minifier Compressor
- Online MD5 Hash Generator
- Online SHA-1, SHA-256, SHA-512 Generator
- Online Base64 Encoder/Decoder
- Online CRC-32 Calculator
- Online Triple DES Encryptor/Decryptor
- More Free Online Tools for Devs
- Database Administration Tutorials
- Programming Tutorials & IT News
- Linux & DevOps World
- Ebook Reviews
- eSport Matches, Skills Tutorials & News
- Entertainment & General News
- All the Free, Online Tools you need