C#richtextbox并读取大文件

时间:2011-11-01 20:55:56

标签: c# file richtextbox

我正在阅读一个巨大的文件(数百MB)并将其显示在richtext框中。当我运行它时,我的程序会冻结。任何人都可以给我建议吗?是因为我读文件的方式?还是因为richtextbox有一些限制?我试图增加maxLength属性2147483647,但它说“属性值无效不是INt32的有效值”。我的应用是64位所以为什么会这样?以及如何使int32成为64?

            StreamReader sr = new StreamReader(file_name1);
            string myLine;
            while ((myLine = sr.ReadLine()) != null)
            {
                richTextBox1.AppendText(myLine+"\n");
                count_lines++;

            }
            sr.Close();
            textBox2.Text = count_lines.ToString(); ;

1 个答案:

答案 0 :(得分:0)

(我会回复你的评论作为答案,以提高可读性)

问:(作者John Ryann)

  

是否可以为richTextBox.LoadFile应用模式   还是File.ReadAllText()?这意味着只能获得线条   哪个/哪个包含某个字符串?

StreamReader sr = new StreamReader("@c:\MyBigFile.log");

string line = sr.ReadLine();
while (line != null)
{
    if(line.Contains("Error"))
    {
        richTextBox.Text += line + Environment.NewLine;
    }

    line = sr.ReadLine();
}
sr.Close();