/// <summary> /// writes <see cref="SettingsContainer"/> object JSON serialization to /// data file. /// </summary> /// <exception cref="BackupFileCreateException"> /// <seealso cref="FileIOUtils.WriteTextToFileWithBackup(string, string, string)"/> /// </exception> /// <exception cref="BackupFileRenameException"> /// <seealso cref="FileIOUtils.WriteTextToFileWithBackup(string, string, string)"/> /// </exception> /// <exception cref="BackupFileDeleteException"> /// <seealso cref="FileIOUtils.WriteTextToFileWithBackup(string, string, string)"/> /// </exception> private void writeSettingsContainerDataToFile() { // get SettingsContainer JSON string string settingsContainerJsonString = JsonUtils.SerializeObject(this.settingsContainer); // write SettingsContainer JSON string to data file FileIOUtils.WriteTextToFileWithBackup( SETTINGS_FILE_PATH, settingsContainerJsonString, BACKUP_SETTINGS_FILE_PATH); }
/// <summary> /// writes <see cref="UserDefinedCommandContainer"/> object JSON serialization to /// data file. /// </summary> /// <exception cref="BackupFileCreateException"> /// <seealso cref="FileIOUtils.WriteTextToFileWithBackup(string, string, string)"/> /// </exception> /// <exception cref="BackupFileRenameException"> /// <seealso cref="FileIOUtils.WriteTextToFileWithBackup(string, string, string)"/> /// </exception> /// <exception cref="BackupFileDeleteException"> /// <seealso cref="FileIOUtils.WriteTextToFileWithBackup(string, string, string)"/> /// </exception> private void writeUserDefinedCommandContainerDataToFile() { // get UserDefinedCommandContainer JSON string string userDefinedCommandContainerJsonString = JsonUtils.SerializeObject( this.userDefinedCommandContainer); // write UserDefinedCommandContainer JSON string to fiel FileIOUtils.WriteTextToFileWithBackup( USER_DEFINED_COMMANDS_FILE_PATH, userDefinedCommandContainerJsonString, BACKUP_USER_DEFINED_COMMANDS_FILE_PATH); }
/// <summary> /// synchronously writes <paramref name="content"/> to file at location <paramref name="filePath"/>. /// if file at <paramref name="filePath"/> does not exist, /// creates a new file containing <paramref name="content"/>. /// if file exists, overrwrites existing file safely. /// </summary> /// <seealso cref="FileIOUtils.WriteTextToFile(string, string, string)"/> /// <param name="filePathWithoutExtension"></param> /// <param name="fileExtension"></param> /// <param name="content"></param> /// <exception cref="BackupFileCreateException"> /// <seealso cref="FileIOUtils.WriteTextToFile(string, string, string)"/> /// </exception> /// <exception cref="FileRenameException"> /// <seealso cref="FileIOUtils.WriteTextToFile(string, string, string)"/> /// </exception> /// <exception cref="BackupFileDeleteException"> /// <seealso cref="FileIOUtils.WriteTextToFile(string, string, string)"/> /// </exception> public void WriteTextToFile( string filePathWithoutExtension, string fileExtension, string content) { string filePath = filePathWithoutExtension + fileExtension; string backupFilePath = getBackupFilePath(filePathWithoutExtension); try { FileIOUtils.WriteTextToFileWithBackup(filePath, content, backupFilePath); } catch (BackupFileWriteException backupFileWriteException) { // backup file could not be renamed to requested file path if (backupFileWriteException is BackupFileRenameException) { BackupFileRenameException backupFileRenameException = backupFileWriteException as BackupFileRenameException; try { // try renaming backup file to to requested file path FileIOUtils.RenameFile(backupFileRenameException.BackupFilePath, filePath); } catch (FileRenameException) // renaming failed again { throw backupFileWriteException; // throw original exception } } else { throw backupFileWriteException; // throw original exception } } }