我正在使用这段代码将文本文件导入我的列表框
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Text Files|*.txt";
openFileDialog1.Title = "Select a Text file";
openFileDialog1.FileName = "";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string file = openFileDialog1.FileName;
string[] text = System.IO.File.ReadAllLines(file);
foreach (string line in text)
{
listBox2.Items.Add(line);
}
listBox2.Items.Add("");
}
它适用于 10 行左右的小文本文件,但是当我尝试导入更大的列表(4-5 兆字节)时,程序没有响应并且崩溃了。
有什么帮助吗?
最佳答案
在 C# 中使用 BufferedStream 类来提高性能。
http://msdn.microsoft.com/en-us/library/system.io.bufferedstream.aspx
关于C# ListBox 从文本文件导入时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13405426/