private void checkBox_Click(object sender, RoutedEventArgs e) { City = this.CityText.Text; if (checkBox.IsChecked == true) { if (loadedData.cityExists(City) == true) { if (PomocniFavourites.Contains(City.ToUpper()) == false) { Favourites.Add(City); PomocniFavourites.Add(City.ToUpper()); if (Favourites.Count() == 6) { Favourites.RemoveAt(0); PomocniFavourites.RemoveAt(0); } this.errorMessage.Content = ""; } else { this.errorMessage.Content = "City is already added to favourites."; } } else { this.errorMessage.Content = "City couldn't be added to favourites. Please check the spelling and internet connection."; } } else { this.errorMessage.Content = ""; } WriteFavorites(); }
public void ReadFavourites() { string currentDirectory = Directory.GetCurrentDirectory(); string path = System.IO.Path.Combine(currentDirectory, "Favourites.txt"); if (!File.Exists(path)) { using (StreamWriter sw = File.CreateText(path)) { sw.Close(); } } try { using (StreamReader sr = File.OpenText(path)) { string s = ""; while ((s = sr.ReadLine()) != null) { Favourites.Add(s); PomocniFavourites.Add(s.ToUpper()); } } } catch (FileNotFoundException fnf) { Console.WriteLine(fnf.ToString()); } }