private void Form1_Load(object sender, EventArgs e) { RecipeManagerData data = null; if (!File.Exists($@"{filePath}")) { DialogResult res = MessageBox.Show($"File '{filePath}' was not found. Would you like to load data from different location?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (res == DialogResult.Yes) { OpenFileDialog openFile = new OpenFileDialog(); openFile.Filter = "Xml (*.dat)|*.dat|All Files (*.*)|*.*"; openFile.FilterIndex = 1; if (openFile.ShowDialog() == DialogResult.OK) { filePath = openFile.FileName; data = RecipeDataManager.LoadData(filePath); } } } else { data = RecipeDataManager.LoadData(filePath); } _storage.SetData(data); }
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { DialogResult res = MessageBox.Show("Save this file?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); switch (res) { case DialogResult.Yes: var data = new RecipeManagerData { Ingredients = _storage.GetIngredient().ToList(), Recipes = _storage.GetRecipe().ToList(), Groups = _storage.GetGroups().ToList() }; if (!File.Exists(filePath)) { SaveFileDialog saveFile = new SaveFileDialog(); saveFile.Filter = "Xml (*.dat)|*.dat|All Files (*.*)|*.*"; if (saveFile.ShowDialog() == DialogResult.OK) { filePath = saveFile.FileName; RecipeDataManager.SaveData(filePath, data); } } else { RecipeDataManager.SaveData(filePath, data); } break; case DialogResult.No: break; } }
private void Form1_Load(object sender, EventArgs e) { RecipeManagerData data = null; if (!File.Exists($@"{filePath}")) { DialogResult res = MessageBox.Show($"Файл '{filePath}' не найден. Возможно файл поменял своё расположение?", "Подтверждение", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (res == DialogResult.Yes) { OpenFileDialog openFile = new OpenFileDialog(); openFile.Filter = "Xml (*.dat)|*.dat|All Files (*.*)|*.*"; openFile.FilterIndex = 1; if (openFile.ShowDialog() == DialogResult.OK) { filePath = openFile.FileName; data = RecipeDataManager.LoadData(filePath); } } } else { data = RecipeDataManager.LoadData(filePath); } _storage.SetData(data); }
public static void SaveData(string path, RecipeManagerData storage) { using (var stream = new FileStream(path, FileMode.Create)) { var serializer = new XmlSerializer(typeof(RecipeManagerData)); serializer.Serialize(stream, storage); } }
public void SetData(RecipeManagerData data) { if (data != null) { _ingredient.SetList(data.Ingredients); _recipe.SetList(data.Recipes); _group.SetList(data.Groups); } }