Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用FileStream和以下选项c读取文本文件的实际内容#_C#_Filestream - Fatal编程技术网

C# 使用FileStream和以下选项c读取文本文件的实际内容#

C# 使用FileStream和以下选项c读取文本文件的实际内容#,c#,filestream,C#,Filestream,我需要在C#中使用FileStream和下面提到的选项打开一个文本文件 var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 64 * 1024,

我需要在C#中使用FileStream和下面提到的选项打开一个文本文件

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从缓存读取文本文件

System.IO.File.ReadAllText()
。。。这还不够好

请有人给我写一个简单的sub,其中包含了这些要求,因为到目前为止,我看到的例子都涉及到字节和缓冲区(我现在确实需要处理这个领域)的工作,并将其保留

谢谢

您可以使用从流中读取:

string contents;
using(var sr = new StreamReader(fileStream))
{
   contents = sr.ReadToEnd();
}
可能是这样的:

    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

对不起,如果这已经被回答了,这里的人张贴得很快

File.ReadAllBytes

理论上两者都将使用windows文件缓存


阅读了解更多信息,了解文件标记、无缓冲的一些限制,并阅读

这是我使用FileStream读取字节数组时所做的操作,效果很好。为了技术清晰,我扩展了名称空间。您应该根据您的场景选择文件共享文件访问参数

    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));
    }
如何将文件内容读取为字符串而不是字节数组?

 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();
         }
     }
为什么StreamReader比使用所需编码将字节[]转换为字符串更好?

 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();
         }
     }
StreamReader类功能强大,因为它将检查头字节并为您确定编码方案。您不必使用System.Text.Encoding.UTF8.GetString()

为什么使用FileStream,为什么不使用System.IO.File.ReadAllBytes/ReadAllText? 我想要精确控制文件锁定机制。在我的场景中,我有一个进程,在一个文件夹中轮询传入的文件。我想确保当我读取文件时,另一端运行缓慢的进程已经完成了它的工作,并且该文件已准备好供我的进程读取和分析

将整个文件作为单个字节数组读取有什么缺点?
如果您打算一次读取一个非常大的文件,那么您对RAM的要求就更高了。考虑一个渲染电影的应用程序。你有一个100MB的电影文件。您不希望一次读取整个文件。您更希望它一次读取100KB的块,渲染场景,然后转到下一块。这使您的应用程序更具可伸缩性。您的电影应用程序现在更具可扩展性。您可以处理非常大的电影,而不会遇到System.IO.OutOfMemoryException的风险。

“System.IO.File.ReadAllText()…还不够好。”?为什么?@SteveB我想是因为你不能指定共享状态。谢谢Dave,这就完成了。