将文本文件加载到RichTextBox中最快的方法是什么? [英] What is the fastest way to load text file into RichTextBox?
问题描述
我使用OpenFIleDialog将文本文件加载到RichTextBox中.但是当有大量文本(例如大约50-70行的歌曲文本)并且我单击OPEN时,程序将挂起几秒钟(〜3-5).正常吗也许有一些更快的方式或组件来加载文本文件?如果我的问题不合适,则将其删除.谢谢.
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;
}
}
我想也许 ReadAllLines
会阻止它吗?
推荐答案
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?
但是,50-70行是没什么 ..无论您如何阅读,它都应立即飞入.您是从网络共享中读取内容还是导致延迟的其他内容?
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?
现在,我看到了您的代码:删除循环,只需编写一次 richTextBox1.Text = text;
.在循环中分配字符串没有意义,因为您已经使用 ReadAllText
读取了文件的完整内容.
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;
}
这篇关于将文本文件加载到RichTextBox中最快的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!