public int total = 0; public string fileLocation; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { OpenFileDialog openDialog = new OpenFileDialog(); openDialog.Title = "Select A File"; openDialog.Filter = "Text Files (*.txt)|*.txt" + "|" + "All Files (*.*)|*.*"; if (openDialog.ShowDialog() == DialogResult.OK) { fileLocation = openDialog.FileName; label1.Text = "STATUS: Tickets Loaded"; } } private void button2_Click(object sender, EventArgs e) { using (StreamReader sr = new StreamReader(fileLocation)) { while (!sr.EndOfStream) { var counts = sr .ReadLine() .Split(' ') .GroupBy(s => s) .Select(g => new { Word = g.Key, Count = g.Count() }); var wc = counts.SingleOrDefault(c => c.Word == "Quav"); total += (wc == null) ? 0 : wc.Count; } } Console.WriteLine(total); }