Dar*_*ria 5 c# performance text load 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;
}
}
Run Code Online (Sandbox Code Playgroud)
我猜也许会ReadAllLines暗示它?
有一个类似的问题涉及读取/写入文件的最快方式:在.NET中读取/写入磁盘的最快方法是什么?
然而,50-70线是没有 ...无论你如何阅读,它应该立即飞入.您是否正在阅读网络共享或其他导致延迟的事情?
编辑:现在我看到你的代码:删除循环,只写richTextBox1.Text = text;一次.在循环中分配字符串没有意义,因为您已经使用了已读取文件的完整内容ReadAllText.
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
string text = File.ReadAllText(openFileDialog1.FileName);
richTextBox1.Text = text;
}
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)