C#如何使用OpenFileDialogue在lisbox中列出文本文件数据

问题描述 投票:-1回答:1

我正在尝试使用OpenFileDialog从计算机中获取任何文本文件,并在列表框中列出其数据。然后,我想将该列表框的内容排序到一个数组中,然后在另一个文本框中列出它。有人可以帮帮我吗?将不胜感激。

c# arrays visual-studio listbox openfiledialog
1个回答
0
投票

我还没有测试过。也许您会得到一个想法。

ListBox listBox = new ListBox();

using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
    openFileDialog.InitialDirectory = "c:\\";
    openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        //Read the contents of the file into a stream
        var fileStream = openFileDialog.OpenFile();

        using (StreamReader reader = new StreamReader(fileStream))
        {
             while (reader .Peek() >= 0) 
                {
                    listBox.Add(sr.ReadLine());
                }


        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.