问题描述
我正在尝试将150mb的文本文件读入RTF文本框中.
I'm trying to read in a 150mb text file into a Rich Text box.
当前,我正在使用StreamReader遍历文件中的每一行,并将每一行追加到StringBuilder实例.
Currently, I am using a StreamReader to iterate through each line in the file, appending every line to a StringBuilder instance.
这适用于较小的文件,但尝试读取大文件时出现System.OutOfMemory异常.
This works for smaller files, but I get a System.OutOfMemory exception when trying to read large files.
读取150mb文件没有任何问题-有足够的物理内存,并且完全在Windows 32位应用程序地址空间之内.
I don't see any problems with reading a 150mb file - there is plenty of physical memory and that's well within the Windows 32-bit application address space.
如果这里有人知道如何执行此操作,将不胜感激.
If anyone here has any idea how to do this, It would be greatly appreciated.
我将在代码末尾附加
谢谢.
StringBuilder sb = new StringBuilder();
using (StreamReader sr = new StreamReader(fileLocation))
{
string line;
while ((line = sr.ReadLine()) != null)
{
sb.AppendLine(line);
}
}
return sb;
推荐答案
使用RichTextBox.LoadFile
http://msdn.microsoft.com/zh-CN/library/system.windows.forms.richtextbox.loadfile.aspx
我不确定为什么要将整个文本加载到StringBuilder
.或者,您可以将FileStream
传递给LoadFile
,这将为您呈现大文件.
I'm not sure why you would want to load the entire text to a StringBuilder
. Alternatively you could pass a FileStream
to LoadFile
which would render the large file for you.
这篇关于C#-将大(150MB)文本文件读入RTF文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!