Accessing a file whom 200 other user's are trying to access at same time as well

StackOverflow https://stackoverflow.com/questions/19047867

  •  29-06-2022
  •  | 
  •  

Вопрос

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 ?

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top