private void CreateBook() { string allBooks = GetBooks(); int id = GetIdValue(); Book newBook = new Book(id, nameTextBox.Text, authorTextBox.Text, publisherTextBox.Text, dateTimePickerPublished.Value); if (CheckForNullorEmpty(newBook)) { using (FileStream fStream = new FileStream(tempDir + "BookDetails.csv", FileMode.Open)) { using (CryptoStream cStream = new CryptoStream(fStream, new AesManaged().CreateEncryptor(_key, _iv), CryptoStreamMode.Write)) { using (StreamWriter w = new StreamWriter(cStream)) { var line = string.Format(newBook.Id.ToString() + "," + newBook.Name + "," + newBook.Author + "," + newBook.Publisher + "," + newBook.DatePublished.ToString() + "," + newBook.DatetimeInserted.ToString()); allBooks = allBooks + line; w.WriteLine(allBooks); w.Flush(); w.Close(); _bookControl.ReadInBooks(); this.Close(); } } } } else { MessageBox.Show("Invaild Please make sure all fields are filled", "Error", MessageBoxButtons.OK); } }
private void UpdateBookChanged() { int id = int.Parse(_itemId.SubItems[0].Text); List <String> lines = new List <String>(); byte[] key = keyClass.GetPrivateKey(_aesEncrypt); byte[] iv = keyClass.GetIV(_aesEncrypt); Book newBook = new Book(id, nameTextBox.Text, authorTextBox.Text, publisherTextBox.Text, datePublishedPicker.Value); if (CheckForNullorEmpty(newBook)) { using (FileStream fStream = new FileStream(tempDir + "BookDetails.csv", FileMode.Open)) { using (CryptoStream cStream = new CryptoStream(fStream, new AesManaged().CreateDecryptor(key, iv), CryptoStreamMode.Read)) { using (StreamReader reader = new StreamReader(cStream)) { String line; while ((line = reader.ReadLine()) != null) { if (line.Contains(",")) { String[] split = line.Split(','); if (split[0].Contains(_itemId.SubItems[0].Text)) { line = string.Format(newBook.Id.ToString() + "," + newBook.Name + "," + newBook.Author + "," + newBook.Publisher + "," + newBook.DatePublished.ToString() + "," + newBook.DatetimeInserted.ToString()); } } lines.Add(line); } } } } using (FileStream fStream = new FileStream(tempDir + "BookDetails.csv", FileMode.Open)) { using (CryptoStream cStream = new CryptoStream(fStream, new AesManaged().CreateEncryptor(key, iv), CryptoStreamMode.Write)) { using (StreamWriter w = new StreamWriter(cStream)) { foreach (String line in lines) { w.WriteLine(line); } } } } _bookControl.ReadInBooks(); this.Close(); } else { MessageBox.Show("Invaild Please make sure all fields are filled", "Error", MessageBoxButtons.OK); } }