使用FileStream和这些选项c#读取文本文件的实际内容 [英] Read the actual contents of text file using FileStream and these options c#
问题描述
我需要使用FileStream并使用下面提到的选项在C#中打开一个文本文件
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);
文本文件包含"1"或"0",获得结果后,我将文本文件的内容分配给字符串变量.如果您有兴趣,我需要上面的选项,以避免Windows从缓存中读取文本文件.
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()
...还不够好.
有人会好心地写一个简单的sub来满足我的这些要求,因为到目前为止我看到的示例涉及使用字节和缓冲区(此时我确实需要处理的一个区域),然后离开就是那个.
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.
谢谢
推荐答案
也许是这样的:
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);
如果文件包含1,则assignedvariable将为true,如果文件为0,则为false.
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.
这篇关于使用FileStream和这些选项c#读取文本文件的实际内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!