本文介绍了在c sharp中写入较大(50mb)字节的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想在文件中写入更大(50mb)的字节,以便在我的应用程序中下载日志目的。我有时会失去内存异常。为了避免内存不足异常我用Google搜索并找到下面的代码。



 使用(StringReader file =  new  StringReader(result))
{
var line = file.ReadLine();
while (line!= null
{
if (cancelLog)
{
GC.Collect();
return ;
}
File.AppendAllText(fileName,line + Environment.NewLine, new UTF8Encoding());
line = file.ReadLine();
}
}





但是编写文件需要很长时间。任何人都可以告诉我在较大的文件情况下避免内存不足的正确方法。



提前致谢。

解决方案

Hi All,
I want to write larger (50mb)bytes to file for download log purpose in my application. I am getting out of memory exception some time. To avoid Out of memory exception i googled and find the below code.

using (StringReader file = new StringReader(result))
               {
                   var line = file.ReadLine();
                   while (line != null)
                   {
                       if (cancelLog)
                       {
                           GC.Collect();
                           return;
                       }
                       File.AppendAllText(fileName, line + Environment.NewLine, new UTF8Encoding());
                       line = file.ReadLine();
                   }
               }



But it takes long time to write the file. Can anyone please tell me the proper way to avoid out of memory exception in larger file situation.

Thanks in advance.

解决方案


这篇关于在c sharp中写入较大(50mb)字节的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 20:36