public LocksFromFile() { string FileName = "locks.txt"; string LineFromFile; List <string> Challenges; try { using (StreamReader MyStream = new StreamReader(FileName)) { LineFromFile = MyStream.ReadLine(); while (LineFromFile != null) { Challenges = LineFromFile.Split(';').ToList(); Lock LockFromFile = new Lock(); foreach (var C in Challenges) { List <string> Conditions = new List <string>(); Conditions = C.Split(',').ToList(); LockFromFile.AddChallenge(Conditions); } Locks.Add(LockFromFile); LineFromFile = MyStream.ReadLine(); } } } catch { Console.WriteLine("File not loaded"); } }
public static void GetCountriesFromFile(DictionaryCountries dictionaryCountries) { try { Console.WriteLine("Reading Countries:\n"); int i = 0; using (StreamReader sr = new StreamReader(Const.Path, System.Text.Encoding.Default)) { string LineFromFile; while ((LineFromFile = sr.ReadLine()) != null) { if (i == 0) { i++; continue; } List<string> tmp = LineFromFile.Split('\t').ToList(); Console.WriteLine($"{i, 3}. {tmp.First(), -20} {tmp.Last(), -20}"); dictionaryCountries.countries.Add(i, new Country(tmp.First(), bool.Parse(tmp.Last()))); i++; } } } catch (Exception e) { Console.WriteLine(e.Message); } }
static void Main(string[] args) { //Task1 string LineFromFile; string Number = ""; string Person = ""; string AllNumbers = ""; string[] SplitLine = new string[2]; Dictionary <String, String> PhoneBook = new Dictionary <String, String>(); StreamReader FileReader = new StreamReader("D:\\phones.txt"); while ((LineFromFile = FileReader.ReadLine()) != null) { SplitLine = LineFromFile.Split('-'); Person = SplitLine[0]; Number = SplitLine[1]; Person = Person.Trim(); Number = Number.Trim(); PhoneBook.Add(Person, Number); } foreach (KeyValuePair <string, string> pair in PhoneBook) { AllNumbers = AllNumbers + pair.Value + Environment.NewLine; } File.WriteAllText("D:\\phones2.txt", AllNumbers); //Task2 bool Result = false; string ResultStr = ""; Console.Write("Please enter the name for search in the database: "); string Input = Console.ReadLine(); foreach (KeyValuePair <string, string> pair in PhoneBook) { if (Input == pair.Key) { Result = true; ResultStr = pair.Value; } } if (Result == true) { Console.WriteLine("The phone number of the person is {0}.", ResultStr); } else { Console.WriteLine("You typed the wrong ID."); } //Task3 string Median = ""; string NewOutput = ""; foreach (KeyValuePair <string, string> pair in PhoneBook) { if (pair.Value.StartsWith("8")) { Median = "+3" + pair.Value; } else { Median = pair.Value; } NewOutput += pair.Key + " - " + Median + Environment.NewLine; File.WriteAllText("D:\\new.txt", NewOutput); } Console.ReadLine(); }