C# ListBox 从文本文件导入时崩溃

标签 c# import crash listbox

我正在使用这段代码将文本文件导入我的列表框

        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/

相关文章:

c# - IronPython 如何访问 C# 中定义的公共(public)静态属性?

C++ 动态 boolean 数组导致崩溃

iphone - 保存 View 问题中的两个 PickerViews?

c# - 我应该将 Unity 容器传递给我的依赖项吗?

c# - 在运行时执行隐式转换

c# - 如何在执行前预览 PowerShell 命令?

typescript :重复标识符 'IteratorResult'

python - 类如何生成根据类属性而变化的回调函数?

c# - 非常简单的 ASP.NET 3.5 应用程序的问题,使用 C#

python - Python Tkinter程序崩溃时忘记了()