3

我正在使用此代码将文本文件导入我的 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 兆字节)程序没有响应并且崩溃了。

有什么帮助吗?

4

5 回答 5

2

使用 C# 中的 BufferedStream 类来提高性能。
http://msdn.microsoft.com/en-us/library/system.io.bufferedstream.aspx

于 2012-11-15T20:22:09.597 回答
1

通过使用这个:

string[] text = System.IO.File.ReadAllLines(file);
listBox1.Items.AddRange(text);

而不是这个:

string[] text = System.IO.File.ReadAllLines(file);
foreach (string line in text)
{
       listBox2.Items.Add(line);
}

您将至少加快执行速度 10-15 倍,因为您不会在每个 Item 插入时使 listBox 无效。我已经测量了几千行。

瓶颈也可能是ReadAllLines您的文本行数过多。即使我不明白为什么要插入这么多行,用户能否找到他/她需要的行?

编辑好然后我建议你使用BackgroundWorker,这里是代码:

首先初始化 BackGroundWorker:

 BackgroundWorker bgw;
        public Form1()
        {
            InitializeComponent();
            bgw = new BackgroundWorker();
            bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
            bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
        }

然后你在你的方法中调用它:

private void button1_Click(object sender, EventArgs e)
        {
            if (!bgw.IsBusy)
            {
                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;
                    listView1.BeginUpdate();
                    bgw.RunWorkerAsync(file);
                }
            }
            else
                MessageBox.Show("File reading at the moment, try later!");
        }


        void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            listView1.EndUpdate();
        }
        void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            string fileName = (string)e.Argument;
            TextReader t = new StreamReader(fileName);
            string line = string.Empty;
            while ((line = t.ReadLine()) != null)
            {
                string nLine = line;
                this.Invoke((MethodInvoker)delegate { listBox1.Items.Add(nLine); });
            }
        }

它会在读取时添加每一行,您将拥有响应式 UI,并且在完成加载之前,行不会影响 listBox。

于 2012-11-15T20:30:26.233 回答
0

可以使用流来存储数据:

class Test
{

public static void Main()
{
    string path = @"c:\temp\MyTest.txt";

    //Create the file. 
    using (FileStream fs = File.Create(path))
    {
        AddText(fs, "This is some text");
        AddText(fs, "This is some more text,");
        AddText(fs, "\r\nand this is on a new line");
        AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n");

        for (int i=1;i < 120;i++)
        {
            AddText(fs, Convert.ToChar(i).ToString());

        }
    }

    //Open the stream and read it back. 
    using (FileStream fs = File.OpenRead(path))
    {
        byte[] b = new byte[1024];
        UTF8Encoding temp = new UTF8Encoding(true);
        while (fs.Read(b,0,b.Length) > 0)
        {
            Console.WriteLine(temp.GetString(b));
        }
    }
}

private static void AddText(FileStream fs, string value)
{
    byte[] info = new UTF8Encoding(true).GetBytes(value);
    fs.Write(info, 0, info.Length);
}

}

然后你的事件处理程序

 privateasyncvoid Button_Click(object sender, RoutedEventArgs e)
    {
        UnicodeEncoding uniencoding = new UnicodeEncoding();
        string filename = @"c:\Users\exampleuser\Documents\userinputlog.txt";

        byte[] result = uniencoding.GetBytes(UserInput.Text);

        using (FileStream SourceStream = File.Open(filename, FileMode.OpenOrCreate))
        {
            SourceStream.Seek(0, SeekOrigin.End);
            await SourceStream.WriteAsync(result, 0, result.Length);
        }
    }
于 2012-11-15T20:22:47.163 回答
0

它可能根本没有完成它的工作,你应该等待更多。试试这个解决方案:

http://www.bytechaser.com/en/articles/f3a3niqyb7/display-large-lists-in-listview-control-quickly.aspx

于 2012-11-15T20:26:01.413 回答
0

您的应用程序变得无响应,因为它正在等待ReadAllLines方法完成并阻塞 UI 线程。您可能希望在单独的线程上读取文件以避免阻塞 UI。我不能保证下面的代码不会出错,但它应该让你知道如何解决这个问题。

首先,您需要一种方法将项目附加到ListBox

private void AddListBoxItem(string item)
{
    if(!InvokeRequired)
    {
        listBox2.Items.Add(item);
    }
    else
    {
        var callback = new Action<string>(AddListBoxItem);
        Invoke(callback, new object[]{item});
    }
}

上面的方法检查它是否在 UI 线程上执行,如果是,它只是将一个项目添加到listBox2.Items集合中;如果没有,它会从自身创建一个委托并在 UI 线程上调用该委托。

接下来,您需要将读取文件的代码移动到另一个线程并调用AddListBoxItem方法。为了可读性,让我们把它放到一个单独的方法中:

private void AddFileContentsToList(string fileName)
{
    using(var reader = new System.IO.StreamReader(fileName))
    {
        while(!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            AddListBoxItem(line);
        }
    }
}

现在我们将在单独的线程上调用该方法:

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)
{
    var thread = new Thread(AddFileContentsToList);
    thread.Start();
}

希望这可以帮助!

于 2012-11-15T20:43:54.663 回答