简体   繁体   English

将文本文件加载到RichTextBox的最快方法是什么?

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

I load text file into RichTextBox using OpenFIleDialog. 我使用OpenFIleDialog将文本文件加载到RichTextBox中。 But when a large amount of text is (for example song text about 50-70 lines) and I click OPEN program hangs for a few second (~3-5). 但是当大量的文本(例如歌曲文本大约50-70行)和我点击OPEN程序挂起几秒钟(〜3-5)。 Is it normal? 这是正常的吗? Maybe there is some faster way or component to load text file? 也许加载文本文件有一些更快的方法或组件? If my question is an inappropriate just delete it. 如果我的问题不合适,只需将其删除。 Thanx. 感谢名单。

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;
            }
        }

I guess maybe ReadAllLines impeds it? 我想也许ReadAllLines impeds呢?

There is a similar question that deals with the fastest way of reading/writing files: What's the fastest way to read/write to disk in .NET? 有一个类似的问题涉及读取/写入文件的最快方式在.NET中读取/写入磁盘的最快方法是什么?

However, 50-70 lines is nothing .. no matter how you read, it should fly in immediately. 然而,50-70线是没有 ...无论你如何阅读,它应该立即飞入。 Are you maybe reading from a network share or something else that is causing the delay? 您是否正在阅读网络共享或其他导致延迟的事情?

Edit: Now that I see your code: Remove the loop and just write richTextBox1.Text = text; 编辑:现在我看到你的代码了:删除循环,然后写下richTextBox1.Text = text; once. 一旦。 It doesn't make sense to assign the string in the loop since you already read the complete content of the file by using ReadAllText . 由于您已经使用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
}

Remove the for loop, because is useless: 删除for循环,因为没用:

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

text is a string that already contains all the text of the file to pass to the textBox. text是一个字符串,已包含要传递给textBox的文件的所有文本。

Doing: 这样做:

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

you're assigning the text read from the file text.Length-1 times ( Length is the number of characters of the string) and that's useless. 你正在分配从文件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