Question

I am developing a code that will be used by 200 different user's to access a single file.

What would be the best practice for me to read file which is being accessed by large number of users at same time

FileStream stream = File.OpenRead(FileName);
 byte[] contents = new byte[stream.Length];
 stream.Read(contents, 0, (int)stream.Length);
 stream.Close();

Any better way of doing it ?

Was it helpful?

Solution

You should use an overload method and specify file sharing explicitly (otherwise you will encounter UnauthorizedAccess exceptions, when opening multiple streams):

var stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);

As long as its reading only - this should work fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top