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

时光怂恿深爱的人放手 提交于 2019-12-12 10:28:44

问题


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


回答1:


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.




回答2:


You can use a StreamReader to read from the stream:

string contents;
using(var sr = new StreamReader(fileStream))
{
   contents = sr.ReadToEnd();
}



回答3:


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




回答4:


This is what I did to read an array of bytes using FileStream and it worked well. I have expanded the namespaces for the sake of technical clarity. You should chose the FileShare and FileAccess parameters as per your scenario.

    byte[] buffer = null;
    using (System.IO.FileStream stm = new System.IO.FileStream("c:\\YourFile.txt", 
               System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None))
    {
    buffer = new byte[stm.Length];
    stm.Read(buffer, 0, Convert.ToInt32(stm.Length));
    }

How to read the contents of the file as a String and not an array of bytes?

 string buffer = null;
 using (System.IO.FileStream stm = new 
     System.IO.FileStream("c:\\YourFile.txt",System.IO.FileMode.Open,  
     System.IO.FileAccess.Read, System.IO.FileShare.None))
     {
         using (System.IO.StreamReader rdr = new System.IO.StreamReader (stm))
         {
                        buffer=rdr.ReadToEnd();
         }
     }

Why is StreamReader better than converting the byte[] to string using the desired encoding?

The StreamReader class is powerful because it will examine the header bytes and determine the encoding scheme for you. You do not have to use System.Text.Encoding.UTF8.GetString().

Why use FileStream and why not System.IO.File.ReadAllBytes/ReadAllText ? I wanted precise control on the file locking mechanism. In my scenario, I had a process that was polling a folder for incoming files. I wanted to be sure that when I read the file, the slow running process on the other end had completed its job and the file was ready for my process to read and analyze.

What are the drawbacks of reading the entire file as a single array of bytes? If you are going to read a very large file all at once then you are placing a higher demand on your RAM. Think of an application which renders movies. You have a 100MB movie file. You do not want to read the entire file at once. You would rather have it read in chunks of say 100KB at a time, render the scenes and then move on to the next chunk. This makes your application more scalable. Your movie application is now more scalable. You can handle very large movies wihout running into the risk of System.IO.OutOfMemoryException.



来源:https://stackoverflow.com/questions/14134887/read-the-actual-contents-of-text-file-using-filestream-and-these-options-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!