The problem is in the last part:
foreach (var pair in nameCount(openFileDialog1.FileName))
{
MessageBox.Show("{0} {1}\n", pair.Key, pair.Value);
}
1) You don't need the (openFileDialog1.FileName) bit.
2) You need to format your string before you can hand it to a messgae box:
foreach (var pair in nameCount)
{
MessageBox.Show(String.Format("{0} {1}\n", pair.Key, pair.Value));
}
But I would do it slightly differently:
foreach (string key in nameCount.Keys)
{
MessageBox.Show(string.Format("{0} {1}\n", key, nameCount[key]));
}
And I really hope you don't have a lot of names, or you will spend all afternoon pressing the "OK" button!
"Could not find file 'C:\Users\acer\AppData\Local\Temporary Projects\FourthProgramm\bin\Debug\openFileDialog1'."
Did you call ShowDialog on the OpenFileDialog at any time?
Normally it would be:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
var nameCount = new Dictionary<string, int>();
foreach (String s in File.ReadAllLines(openFileDialog1.FileName))
if (nameCount.ContainsKey(s))
{
nameCount[s] = nameCount[s] + 1;
}
else
{
nameCount.Add(s, 1);
}
var output = new StringBuilder();
foreach (var pair in nameCount)
{
MessageBox.Show(String.Format("{0} {1}\n", pair.Key, pair.Value));
}
}
}