我想打开一个文本文件内容并将其添加到列表框中,但是如果我想添加另一个文本文件,如何检查我添加的文件是否已经添加到列表框中。我的意思是我不想重复列表框。
private void openAndAddToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog.Filter = "Text file|*.txt";
openFileDialog.Title = "Open Text";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
List<string> lines = new List<string>();
using (StreamReader r = new StreamReader(openFileDialog.OpenFile()))
{
string line;
while ((line = r.ReadLine()) != null)
{
donutListBox.Items.Add(line);
}
}
}
}
添加一个如果:
if ( !donutListBox.Items.Contains(line) ){
donutListBox.Items.Add(line);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句