I use the following C # code to read a tiny text file through a network share:
string fileContent; using (var stream = File.OpenRead(filePath)) using (var reader = new StreamReader(stream, FileEncoding)) { fileContent = await reader.ReadToEndAsync(); }
Although the text file is very small (less than 10 KB), this operation sometimes takes ~ 7 seconds. When this happens, I noticed that most of the time is spent on
File.OpenRead(filePath)
This is probably due to the fact that Windows should allow file sharing and get file locks over the network. Since this method call is not asynchronous, it blocks my current thread for several seconds.
Is there a safe way to asynchronously read a file from disk that also executes OpenRead asynchronously?
source share