using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Xml; namespace Task_3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// /// Opens File and executes code to show how many duplicates there are /// /// /// private void openFileToolStripMenuItem_Click(object sender, EventArgs e) { //Set up streamreader StreamReader reader; //Create filter for txt files openFileDialog1.Filter = "Text Document|*.txt|All Files|*.*"; //List to store all names List namesList = new List (); //List to store duplicate names List duplicateCheckList = new List(); //Try catch for input try { //If ok is pressed if(openFileDialog1.ShowDialog() == DialogResult.OK) { //Reads file reader = File.OpenText(openFileDialog1.FileName); //Try catch for input try { //Execute until the end of stream while (!reader.EndOfStream) { //Reads each line and stores into 2 lists and displays the name string line = reader.ReadLine(); namesList.Add(line); duplicateCheckList.Add(line); listBoxAllNames.Items.Add(line); } //Set a counter for number of duplicates int counter = 0; //Goes through each name is original list foreach (string iNameList in namesList) { //Goes through each name in duplicate list foreach (string iDuplicateCheck in duplicateCheckList) { //Checks if the original name list matches the duplicate list if (iNameList == iDuplicateCheck) { //Counts the duplicate counter++; } } //Checks if a duplication has occured if (counter != 0) { //Displays the dupliacte items with the number of duplicates listBoxDuplicates.Items.Add(iNameList + ": " + counter); } //Removes all the duplicates from the duplicated list so it doesn't get show again duplicateCheckList.RemoveAll(x => x == iNameList); //Set counter to 0 for next counter = 0; } } catch (Exception ex) { //Displays error message MessageBox.Show(ex.Message); //Clears listboxes listBoxAllNames.Items.Clear(); listBoxDuplicates.Items.Clear(); } } } catch (Exception ex) { //Displays error message MessageBox.Show(ex.Message ); //Clears listboxes listBoxAllNames.Items.Clear (); listBoxDuplicates.Items.Clear(); } } } }