public static void editFile(string existingFilePath) { BinaryFormatter formatter = new BinaryFormatter(); FileStream streamOfLocalFile = new FileStream(existingFilePath, FileMode.Open, FileAccess.Read); EncryptedFile file = (EncryptedFile)formatter.Deserialize(streamOfLocalFile); streamOfLocalFile.Close(); byte[] decryptedData = Decrypt.decryptLocalFile(file); NewFileWindow newFileWindow = new NewFileWindow(); newFileWindow.Title.Text = Path.GetFileName(existingFilePath); newFileWindow.Title.IsEnabled = false; newFileWindow.Content.Text = Encoding.UTF8.GetString(decryptedData); newFileWindow.ShowDialog(); byte[] newFileData = Encoding.UTF8.GetBytes(newFileWindow.Content.Text); newFileWindow.Close(); var outFile = Encrypt.encryptDataForLocalFile(newFileData); File.Delete(existingFilePath); FileStream fileStream = File.Create(existingFilePath); BinaryFormatter binFormat = new BinaryFormatter(); binFormat.Serialize(fileStream, outFile); fileStream.Close(); return; }
private void NewTextFileButton_Click(object sender, RoutedEventArgs e) { if (Tree.SelectedItem != null) { string filePath = ((TreeViewItem)Tree.SelectedItem).Tag.ToString(); FileAttributes fileAttributes = File.GetAttributes(filePath); if (!fileAttributes.HasFlag(FileAttributes.Directory)) { MessageBox.Show("You must select a folder!"); return; } NewFileWindow window = new NewFileWindow(); window.ShowDialog(); string content = ((NewFileWindow)Application.Current.MainWindow).Content.Text; string fileName = ((NewFileWindow)Application.Current.MainWindow).Title.Text; Application.Current.MainWindow = this; if (File.Exists(filePath + "\\" + fileName)) { MessageBox.Show("File with the same name on that location already exists"); return; } ///sucess UserActions.createNewTextFile(content, fileName, filePath); } else { MessageBox.Show("No location has been selected"); return; } refresh(); }