private void ChooseYourFighter(string s, bool saving) { if (saving) { if (s.IndexOf(".dat") > 0) { BinaryFormatter formatter = new BinaryFormatter(); using (FileStream fs = new FileStream(s, FileMode.OpenOrCreate)) { formatter.Serialize(fs, Models); } MessageBox.Show("Успешно!"); } else if (s.IndexOf(".json") > 0) { DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(typeof(List <Phones>), new Type[] { typeof(Apple), typeof(Samsung), typeof(Xiaomi), typeof(Huawei) }); using (FileStream fs = new FileStream(s, FileMode.OpenOrCreate)) { jsonFormatter.WriteObject(fs, Models); } MessageBox.Show("Успешно!"); } else if (s.IndexOf(".txt") > 0) { string text = ""; for (int i = 0; i < Models.Count; i++) { if (Models[i] is Apple) { Apple curr = Models[i] as Apple; text += $"'Apple'|'{curr.Name}'|'{curr.RepairStartDate}'|'{curr.Number}'|'{curr.EstimatedCost}'|'{curr.Address.City}'|'" + $"{curr.Address.Street}'|'{curr.Address.Building}'|'{curr.PhoneModel}'|'{curr.Restored}'\r\n"; } else if (Models[i] is Samsung) { Samsung curr = Models[i] as Samsung; text += $"'Samsung'|'{curr.Name}'|'{curr.RepairStartDate}'|'{curr.Number}'|'{curr.EstimatedCost}'|'{curr.Address.City}'|'" + $"{curr.Address.Street}'|'{curr.Address.Building}'|'{curr.PhoneModel}'|'{curr.Restored}'\r\n"; } else if (Models[i] is Xiaomi) { Xiaomi curr = Models[i] as Xiaomi; text += $"'Xiaomi'|'{curr.Name}'|'{curr.RepairStartDate}'|'{curr.Number}'|'{curr.EstimatedCost}'|'{curr.Address.City}'|'" + $"{curr.Address.Street}'|'{curr.Address.Building}'|'{curr.PhoneModel}'\r\n"; } else if (Models[i] is Huawei) { Huawei curr = Models[i] as Huawei; text += $"'Huawei'|'{curr.Name}'|'{curr.RepairStartDate}'|'{curr.Number}'|'{curr.EstimatedCost}'|'{curr.Address.City}'|'" + $"{curr.Address.Street}'|'{curr.Address.Building}'|'{curr.PhoneModel}'\r\n"; } else { Phones curr = Models[i] as Phones; text += $"'Не уточнено'|'{curr.Name}'|'{curr.RepairStartDate}'|'{curr.Number}'|'{curr.EstimatedCost}'|'{curr.Address.City}'|'" + $"{curr.Address.Street}'|'{curr.Address.Building}'\r\n"; } } using (FileStream fs = new FileStream(s, FileMode.OpenOrCreate)) { byte[] array = Encoding.Default.GetBytes(text); fs.Write(array, 0, array.Length); } MessageBox.Show("Успешно!"); } else { MessageBox.Show("Что-то пошло не так!"); } } else { if (s.IndexOf(".dat") > 0) { BinaryFormatter formatter = new BinaryFormatter(); using (FileStream fs = new FileStream(s, FileMode.OpenOrCreate)) { Models = (List <Phones>)formatter.Deserialize(fs); } MessageBox.Show("Успешно!"); } else if (s.IndexOf(".json") > 0) { DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(typeof(List <Phones>), new Type[] { typeof(Apple), typeof(Samsung), typeof(Xiaomi), typeof(Huawei) }); using (FileStream fs = new FileStream(s, FileMode.OpenOrCreate)) { Models = (List <Phones>)jsonFormatter.ReadObject(fs); } MessageBox.Show("Успешно!"); } else if (s.IndexOf(".txt") > 0) { using (StreamReader sr = new StreamReader(s, System.Text.Encoding.Default)) { while (!sr.EndOfStream) { string[] line = sr.ReadLine().Split('|'); string clName = line[0].Substring(1, line[0].Length - 2); string name = line[1].Substring(1, line[1].Length - 2); DateTime repairStartDate = Convert.ToDateTime(line[2].Substring(1, line[2].Length - 2)); string number = line[3].Substring(1, line[3].Length - 2); int estimatedCost = Convert.ToInt32(line[4].Substring(1, line[4].Length - 2)); string city = line[5].Substring(1, line[5].Length - 2); string street = line[6].Substring(1, line[6].Length - 2); int building = Convert.ToInt32(line[7].Substring(1, line[7].Length - 2)); string secondparam = ""; if (clName != "Не уточнено") { secondparam = line[8].Substring(1, line[8].Length - 2); } bool restored = false; if (clName == "Apple") { restored = Convert.ToBoolean(line[9].Substring(1, line[9].Length - 2)); } if (clName == "Samsung") { restored = Convert.ToBoolean(line[9].Substring(1, line[9].Length - 2)); } Address currAddr = new Address(city, street, building); Phones phone1 = getModelsTypeByString(clName, name, repairStartDate, number, currAddr, estimatedCost, secondparam, restored); Models.Add(phone1); } } MessageBox.Show("Успешно!"); } else { MessageBox.Show("Что-то пошло не так!"); } } }