[英]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.