Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ЛР5 / Лаб. 5 C# (Вариант 1).docx
Скачиваний:
6
Добавлен:
31.08.2024
Размер:
32.93 Кб
Скачать

If (File.Exists(filename))

{

try

{

file = File.OpenRead(filename); //открывает файл для чтения

BinaryFormatter formatter = new BinaryFormatter();

Student student = (Student)formatter.Deserialize(file);

this.name = student.name;

this.surname = student.surname;

this.dateOfBirth = student.dateOfBirth;

this.FormOfEducation = student.formOfEducation;

this.groupNumber = student.groupNumber;

this.exams = student.exams;

//this.Exams.AddRange(student.Exams);

this.tests = student.tests;

//file.Close();

return true;

}

catch

{

Console.WriteLine("Проблемы при загрузке из файла: " + filename);

//file.Close();

return false;

}

finally

{

file.Close();

}

}

else

{

Console.WriteLine("Файла с таким именем не существует! Файл создаётся.");

try

{

file = File.Create(filename);

//file.Close();

return true;

}

catch

{

Console.WriteLine("Проблемы с загрузской из созданного файла: " + filename);

//file.Close();

return false;

}

finally

{

file.Close();

}

}

}

public bool AddFromConsole()

{

Console.WriteLine("Введите данные: \nНазвание предмета(string)\nОценка(int)\nДата экзамена(dd.mm.yyyy)");

Console.WriteLine("Символы для разделения: !, @, /\n");

Console.Write("-> ");

try

{

string[] args = Console.ReadLine().Split('!', '@', '/');

if (args.Length == 3)

{

Exam exam = new Exam();

exam.subject = args[0];

exam.grade = Convert.ToInt32(args[1]);

exam.examDate = Convert.ToDateTime(args[2]);

this.exams.Add(exam);

return true;

}

else

{

Console.WriteLine("Неверное количество аргументов!");

return false;

}

}

catch

{

Console.WriteLine("Проблемы с преобразованием с консоли!");

return false;

}

}

public static bool Save(string filename, Student obj)

{

Stream file = null;

If (File.Exists(filename))

{

try

{

file = File.Open(filename, FileMode.Create);

BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(file, obj);

//file.Close();

return true;

}

catch

{

Console.WriteLine("Проблемы с сохранением в файл (static): " + filename);

//file.Close();

return false;

}

finally

{

file.Close();

}

}

else

{

try

{

file = File.Create(filename);

file.Close();

file = File.Open(filename, FileMode.Append);

BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(file, obj);

//file.Close();

return true;

}

catch

{

Console.WriteLine("Проблемы с сохранением в созданный файл (static): " + filename);

//file.Close();

return false;

}

finally

{

file.Close();

}

}

}

public static bool Load(string filename, Student obj)

{

Stream file = null;

Соседние файлы в папке ЛР5