将文本文件加载到 RichTextBox 的最快方法是什么?
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一个类似的问题涉及读取/写入文件的最快方式: 在 .NET 中读取/写入磁盘的最快方法是什么?
但是,50-70 行根本不算什么。无论您如何读取,它都应该立即飞入。您是否可能从网络共享或其他导致延迟的原因中读取内容?
编辑:
现在我看到了您的代码:删除循环并只需编写一次
richTextBox1.Text = text;
。在循环中分配字符串没有意义,因为您已经使用ReadAllText
读取了文件的完整内容。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 usingReadAllText
.删除 for 循环,因为没有用:
text
是一个字符串,它已经包含要传递给文本框的文件的所有文本。做法:
您将从文件中读取的文本分配
text.Length-1
次(Length
是字符串的字符数),但这是没有用的。Remove the for loop, because is useless:
text
is a string that already contains all the text of the file to pass to the textBox.Doing:
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.