private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { try { var notes = notesListView.Items. Cast <NoteViewItem>(). Select(i => i.Note). ToArray(); if (notes.Length == 0) { return; } var fileBytes = NoteSerializer.Serialize(notes); using (var inputPasswordForm = new InputPasswordForm()) { if (inputPasswordForm.ShowDialog() == DialogResult.OK) { fileBytes = Aes.Encrypt(fileBytes, inputPasswordForm.PasswordBytes); } } File.WriteAllBytes(Config.NoterFile, fileBytes); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void MainForm_Load(object sender, EventArgs e) { try { if (!File.Exists(Config.NoterFile)) { File.Create(Config.NoterFile); return; } var fileBytes = File.ReadAllBytes(Config.NoterFile); if (fileBytes.Length != 0) { try { using (var inputPasswordForm = new InputPasswordForm()) { if (inputPasswordForm.ShowDialog() == DialogResult.OK) { fileBytes = Aes.Decrypt(fileBytes, inputPasswordForm.PasswordBytes); } } var notes = NoteSerializer.Deserialize(fileBytes); notesListView.AddNotes(notes); } catch (CryptographicException) { if (QuestionAsker.Ask(Config.DecryptionFailedError)) { Environment.Exit(0); } else { File.Delete(Config.NoterFile); } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }