Alex Rivera | Logout

Read the actual contents of text file using FileStream and these options c#

Asked 2013-01-03T07:43:22.780
35

I need to open a text file within C# using FileStream and with the options mentioned below

var fileStream = new FileStream(filePath, 
                                FileMode.Open, 
                                FileAccess.Read, 
                                FileShare.Read, 64 * 1024,
                               (FileOptions)FILE_FLAG_NO_BUFFERING | 
                                  FileOptions.WriteThrough & FileOptions.SequentialScan);

The text file contains a "1" or "0" and after obtaining the results I am going to assign the contents of the text file to a string variable. In case you're interested, I need the above options in order to avoid Windows reading the text files from cache.

System.IO.File.ReadAllText()

... is not good enough.

Would somebody be kind enough to write a simple sub which incorporates these requirements for me please as the examples I've seen so far involve working with bytes and buffers (an area I really need to work on at this time) and leaves it at that.

Thanks

Edit
Report

1 Answer

5
File.ReadAllBytes

or

File.ReadAllText

Will both theoretically use windows file cache.

Read this for more understanding and for some of the restrictions on FILE_FLAG_NO_BUFFERING and also read this for a similar stackoverflow question

answered 2013-01-03T09:03:46.230

Your Answer