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

发布于 2024-10-22 11:22:26 字数 508 浏览 1 评论 0原文

我使用 OpenFIleDialog 将文本文件加载到 RichTextBox 中。但是,当大量文本(例如大约 50-70 行的歌曲文本)并且我单击“打开”时,程序会挂起几秒钟(~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 阻碍了它?

I load text file into RichTextBox using OpenFIleDialog. 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). 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终难遇 2024-10-29 11:22:26

有一个类似的问题涉及读取/写入文件的最快方式: 在 .NET 中读取/写入磁盘的最快方法是什么?

但是,50-70 行根本不算什么。无论您如何读取,它都应该立即飞入。您是否可能从网络共享或其他导致延迟的原因中读取内容?

编辑:
现在我看到了您的代码:删除循环并只需编写一次 richTextBox1.Text = text; 。在循环中分配字符串没有意义,因为您已经使用 ReadAllText 读取了文件的完整内容。

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

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?

However, 50-70 lines is nothing.. no matter how you read, it should fly in immediately. 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; 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.

if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
    string text = File.ReadAllText(openFileDialog1.FileName);
    richTextBox1.Text = text;
}
与酒说心事 2024-10-29 11:22:26
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
}
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
}
似最初 2024-10-29 11:22:26

删除 for 循环,因为没有用:

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

text 是一个字符串,它已经包含要传递给文本框的文件的所有文本。

做法:

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

您将从文件中读取的文本分配 text.Length-1 次(Length 是字符串的字符数),但这是没有用的。

Remove the for loop, because is useless:

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.

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.

半﹌身腐败 2024-10-29 11:22:26
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    richTextBox1.Text = File.ReadAllText(openFileDialog1.FileName);
}
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    richTextBox1.Text = File.ReadAllText(openFileDialog1.FileName);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文