public static bool AddInfo(Info infoToAdd) { try { using (StreamWriter writer = new StreamWriter(FilePath, true)) { writer.WriteLine(infoToAdd); } return true; } catch (Exception) { return false; } }
public static List<Info> TakeInfoFromTheList() { var listOfCapital = new List<Info>(); using (var reader = new StreamReader(Operation.FilePath)) { var currentCapital = reader.ReadLine(); while (currentCapital != null) { var currentCountry = currentCapital.Split('|'); var people = new Info( currentCountry[0].Trim(), currentCountry[1].Trim(), int.Parse(currentCountry[2].Trim())); listOfCapital.Add(people); currentCapital = reader.ReadLine(); } } return listOfCapital; }
protected void addButton_Click(object sender, EventArgs e) { var infoToAdd = new Info( this.capitalTextBox.Text, this.countryTextBox.Text, int.Parse(this.peopleTextBox.Text)); if (Operation.AddInfo(infoToAdd)) { this.labelMessage.Text = "Successfully added"; this.capitalTextBox.Text = string.Empty; this.countryTextBox.Text = string.Empty; this.peopleTextBox.Text = string.Empty; } else { this.labelMessage.ForeColor = System.Drawing.Color.Red; this.labelMessage.Text = "An error occured while adding the country"; } }