我正在尝试使用OpenFileDialog从计算机中获取任何文本文件,并在列表框中列出其数据。然后,我想将该列表框的内容排序到一个数组中,然后在另一个文本框中列出它。有人可以帮帮我吗?将不胜感激。
我还没有测试过。也许您会得到一个想法。
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());
}
}
}
}