public void Save(string path, string password, Encrypter encrypter) { try { string check = generateCheck(); string text = encrypter.Encrypt(check + FIELD_SEPARATOR + this.ToString() + FIELD_SEPARATOR + check, password); System.IO.File.WriteAllText(path, text); this.Path = path; this.modified = false; } catch (ArgumentException) { throw new FileException(Error.NON_VALID_PATH); } catch (PathTooLongException) { throw new FileException(Error.PATH_TOO_LONG); } catch (DirectoryNotFoundException) { throw new FileException(Error.DIRECTORY_NOT_FOUND); } catch (UnauthorizedAccessException) { throw new FileException(Error.UNAUTHORIZED_ACCESS); } catch (FileNotFoundException) { throw new FileException(Error.FILE_NOT_FOUND); } catch (IOException) { throw new FileException(Error.IO_ERROR); } catch (NotSupportedException) { throw new FileException(Error.NON_VALID_PATH); } catch (SecurityException) { throw new FileException(Error.UNAUTHORIZED_ACCESS); } }
private void saveFile() { try { Encrypter encrypter = Settings.SelectedEncrypter; DialogResult result = saveFileDialog.ShowDialog(); if (result == DialogResult.OK) { string path = saveFileDialog.FileName; string password = passwordTextbox.Text; parent.SaveFile(path, password, encrypter); this.Close(); if (exitApplicationAfterSaving) { Application.Exit(); } } } catch (FileException exception) { ShowError(Settings.GetString(exception.Error)); } catch (EncrypterException exception) { ShowError(Settings.GetString(exception.Error)); } }
private void openFile() { try { Encrypter encrypter = Settings.SelectedEncrypter; if (System.IO.Directory.Exists(path)) { DialogResult result = openFileDialog.ShowDialog(); if (result == DialogResult.OK) { parent.OpenFile(openFileDialog.FileName, passwordTextbox.Text, encrypter); } } else //path is file assumed { parent.OpenFile(path, passwordTextbox.Text, encrypter); } this.Close(); } catch (FileException exception) { ShowError(Settings.GetString(exception.Error)); } catch (EncrypterException exception) { ShowError(Settings.GetString(exception.Error)); } }
public static File Open(string path, string password, Encrypter encrypter) { try { string decrypted = encrypter.Decrypt(System.IO.File.ReadAllText(path), password); List <string> fields = decrypted.Split(FIELD_SEPARATOR).ToList(); if (fields.Count >= 2 && fields[0] == fields[fields.Count - 1]) { fields.RemoveAt(0); fields.RemoveAt(fields.Count - 1); File file = new File(); file.Path = path; foreach (string field in fields) { if (field.Length != 0) { file.Add(field); } } file.modified = false; return(file); } else { throw new FileException(Error.CHECKS_DID_NOT_MATCH); } } catch (ArgumentException) { throw new FileException(Error.NON_VALID_PATH); } catch (PathTooLongException) { throw new FileException(Error.PATH_TOO_LONG); } catch (DirectoryNotFoundException) { throw new FileException(Error.DIRECTORY_NOT_FOUND); } catch (UnauthorizedAccessException) { throw new FileException(Error.UNAUTHORIZED_ACCESS); } catch (FileNotFoundException) { throw new FileException(Error.FILE_NOT_FOUND); } catch (IOException) { throw new FileException(Error.IO_ERROR); } catch (NotSupportedException) { throw new FileException(Error.NON_VALID_PATH); } catch (SecurityException) { throw new FileException(Error.UNAUTHORIZED_ACCESS); } }
public void SaveFile(string path, string password, Encrypter encrypter) { file.Save(path, password, encrypter); this.Text = file.Path; ShowInformation(Settings.GetString(this, String.FILE_SAVED.ToString())); }