Difference between FileShare.Read and FileShare.ReadWrite in Read Only Mode

Hakan
Hakan used Ask the Experts™
on
Hi,

I'm using below code for only accessing a file with multiple users at the same time so i try to create a file stream, but i don't know what's changing with File share mode?

Could anyone help me what's the difference between them. Are there any difficulties will occur?

            System.IO.FileStream outputFileStream = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);

Open in new window


            System.IO.FileStream outputFileStream = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);

Open in new window


Thank you.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Eduard GherguArchitect - Coder - Mentor
Commented:
Hi, The Read-Write share allows others to change the file, even though you'll just read it. The Read share will prevent any changes to occur on that specific file (open exclusively by you). Of course, this is valid only for the period of time that you're accessing the file.
Chief Technology Ninja
Distinguished Expert 2018
Commented:
Hi Hakan,

First of all, you can include
using System.IO;

Open in new window

along with your other using directives and then you can just write class name instead of a fully qualified class name.

Now coming to your query, If you do not provide FileShare.Read, then subsequent read request will fail until this stream is closed. If you do not need to write to this file, you can specify both - FileAccess and FileShare - Read access.

Regards,
Chinmay.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial