相关疑难解决方法(0)

C# - 将大文件加载到WPF RichTextBox中?

我需要将一个~10MB范围的文本文件加载到WPF RichTextBox中,但我当前的代码正在冻结UI.我尝试让后台工作人员进行加载,但这似乎也不太好用.

这是我的加载代码.有没有办法改善其表现?谢谢.

    //works well for small files only
    private void LoadTextDocument(string fileName, RichTextBox rtb)
    {
        System.IO.StreamReader objReader = new StreamReader(fileName);

        if (File.Exists(fileName))
        {
                rtb.AppendText(objReader.ReadToEnd());
        }
        else rtb.AppendText("ERROR: File not found!");
        objReader.Close();
    }






    //background worker version. doesnt work well
    private void LoadBigTextDocument(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
        System.IO.StreamReader objReader = new StreamReader(   ((string[])e.Argument)[0]  );
        StringBuilder sB = new StringBuilder("For performance reasons, only the first 1500 lines are displayed. If you need to view the entire …
Run Code Online (Sandbox Code Playgroud)

c# wpf richtextbox

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

c# ×1

richtextbox ×1

wpf ×1