/// <summary> /// Saves the specified document to a file that the user can select from a "Save File" /// dialog. /// </summary> /// <returns> /// <see langword="true"/> if all changes were saved successfully or can be discarded; /// otherwise <see langword="false"/> if there are still changes that need to be saved. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="document"/> is <see langword="null"/>. /// </exception> private bool SaveAs(Document document) { if (document == null) throw new ArgumentNullException(nameof(document)); Logger.Debug(CultureInfo.InvariantCulture, "Saving document \"{0}\" using the Save File dialog.", document.GetName()); var saveFileDialog = SaveFileDialog; if (document.IsUntitled) { saveFileDialog.FileName = document.UntitledName; } else { string path = document.Uri.LocalPath; string fileName = Path.GetFileName(path); string directory = Path.GetDirectoryName(path); saveFileDialog.FileName = fileName; saveFileDialog.InitialDirectory = directory; } saveFileDialog.Filter = document.FileDialogFilter; saveFileDialog.FilterIndex = document.FileDialogFilterIndex; bool? result = saveFileDialog.ShowDialog(); if (result == true) { try { document.Save(new Uri(saveFileDialog.FileName)); RememberRecentFile(document.Uri); UpdateCommands(); return true; } catch (Exception exception) { Logger.Warn(exception, CultureInfo.InvariantCulture, "Could not save file as \"{0}\".", saveFileDialog.FileName); string message = Invariant($"Could not save file.\n\n{exception.Message}"); MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } return false; }
/// <inheritdoc/> public bool Save(Document document) { if (document == null) throw new ArgumentNullException(nameof(document)); if (document.IsDisposed) return true; Logger.Info(CultureInfo.InvariantCulture, "Saving document \"{0}\".", document.GetName()); if (document.IsUntitled) return SaveAs(document); try { document.Save(); RememberRecentFile(document.Uri); UpdateCommands(); return true; } catch (Exception exception) { Logger.Warn(exception, CultureInfo.InvariantCulture, "Could not save file {0}.", document.Uri); string message = Invariant($"Could not save file.\n\n{exception.Message}"); MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); return false; } }