Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 862
user1776480 Avatar asked Jan 03 '13 07:01

user1776480


People also ask

What is the use of FileStream in C#?

The FileStream is a class used for reading and writing files in C#. It is part of the System.IO namespace. To manipulate files using FileStream, you need to create an object of FileStream class. This object has four parameters; the Name of the File, FileMode, FileAccess, and FileShare.

Which method of FileStream class is use for reading line from file?

StreamReader.ReadLine Method (System.IO) Reads a line of characters from the current stream and returns the data as a string.


1 Answers

Maybe something like:

    FileStream fileStream = new FileStream("[path]", FileMode.Open, FileAccess.Read, FileShare.Read, 64 * 1024,         (FileOptions)0x20000000 | FileOptions.WriteThrough & FileOptions.SequentialScan);      string fileContents;     using (StreamReader reader = new StreamReader(fileStream))     {         fileContents = reader.ReadToEnd();     }       bool assignedvariable = Convert.ToBoolean(fileContents); 

assignedvariable will hold true if the file contains 1 and false if it contains 0.

Sorry if this has been answered already people post very fast here.

like image 65
TheKingDave Avatar answered Sep 19 '22 15:09

TheKingDave