簡體   English   中英

將文本文件加載到RichTextBox的最快方法是什么?

[英]What is the fastest way to load text file into RichTextBox?

我使用OpenFIleDialog將文本文件加載到RichTextBox中。 但是當大量的文本(例如歌曲文本大約50-70行)和我點擊OPEN程序掛起幾秒鍾(〜3-5)。 這是正常的嗎? 也許加載文本文件有一些更快的方法或組件? 如果我的問題不合適,只需將其刪除。 感謝名單。

if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string text = File.ReadAllText(openFileDialog1.FileName);
            for (int i = 0; i < text.Length - 1; i++)
            {
                richTextBox1.Text = text;
            }
        }

我想也許ReadAllLines impeds呢?

有一個類似的問題涉及讀取/寫入文件的最快方式在.NET中讀取/寫入磁盤的最快方法是什么?

然而,50-70線是沒有 ...無論你如何閱讀,它應該立即飛入。 您是否正在閱讀網絡共享或其他導致延遲的事情?

編輯:現在我看到你的代碼了:刪除循環,然后寫下richTextBox1.Text = text; 一旦。 由於您已經使用ReadAllText讀取了文件的完整內容,因此在循環中分配字符串沒有意義。

if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
    string text = File.ReadAllText(openFileDialog1.FileName);
    richTextBox1.Text = text;
}
void LoadFileToRTB(string fileName, RichTextBox rtb)
{
      rtb.LoadFile(File.OpenRead(fileName), RichTextBoxStreamType.PlainText); // second parameter you can change to fit for you
      // or
      rtb.LoadFile(fileName);
      // or
      rtb.LoadFile(fileName, RichTextBoxStreamType.PlainText); // second parameter you can change to fit for you
}

刪除for循環,因為沒用:

string text = File.ReadAllText(openFileDialog1.FileName);
richTextBox1.Text = text;

text是一個字符串,已包含要傳遞給textBox的文件的所有文本。

這樣做:

for(int i=0, i < text.Lengt-1; i++)
    richTextBox1.Text = text;

你正在分配從文件text.Length-1次讀取的文本( Length是字符串的字符數),這是無用的。

if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    richTextBox1.Text = File.ReadAllText(openFileDialog1.FileName);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM