我有以下代码,我在其中将数据添加到列表视图,但最终在其中包含冗余项目。请让我知道我哪里错了
private void button2_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
StreamReader sr = new StreamReader("C:\\sample.txt");
string s;
s = sr.ReadLine();
while (s != null)
{
s = sr.ReadLine();
var m = Regex.Match(s, @"^([a-zA-Z._]+)@([\d]+)");
if(m.Success)
{
allcont ac = new allcont();
ac.name = m.Groups[1].Value;
ac.number = m.Groups[2].Value;
con.Add(ac);
foreach (allcont aa in con)
{
ListViewItem i = new ListViewItem(new string[] { aa.name, aa.number });
i.Tag = aa;
listView1.Items.Add(i);
}
s = sr.ReadLine();
}
}
sr.Close();
}
contacts con = new contacts();
public class contacts:List<allcont>
{
}
public class allcont
{
public string name;
public string number;
}
}
我的sample.txt有这个:
wer@123
erty@098
sdf@645
ytu@432
更新:这是我的列表视图显示的数据:
name number
wer 123
wer 123
erty 098
wer 123
erty 098
sdf 645
wer 123
erty 098
sdf 645
wer 123
erty 098
sdf 645
ytu 432