Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RegPreview] Various improvements on how files are saved #37628

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
correctly handle file save errors
  • Loading branch information
htcfreek committed Feb 25, 2025
commit c2887531f1aa4ce18bcccc87fcc7e9a6c83644c2
Original file line number Diff line number Diff line change
@@ -107,7 +107,12 @@ private async void OpenButton_Click(object sender, RoutedEventArgs e)
{
case ContentDialogResult.Primary:
// Save, then continue the file open
SaveFile();
bool success = SaveFile();
if (!success)
{
return;
}

break;
case ContentDialogResult.Secondary:
// Don't save and continue the file open!
@@ -176,7 +181,7 @@ private void SaveButton_Click(object sender, RoutedEventArgs e)
}

// save and update window title
SaveFile();
_ = SaveFile();
_updateWindowTitleFunction(_appFileName);
}

@@ -200,7 +205,7 @@ private async void SaveAsButton_Click(object sender, RoutedEventArgs e)
}

_appFileName = filename;
SaveFile();
_ = SaveFile();
UpdateToolBarAndUI(await OpenRegistryFile(_appFileName));
}

@@ -281,7 +286,12 @@ private async void WriteButton_Click(object sender, RoutedEventArgs e)
{
case ContentDialogResult.Primary:
// Save, then continue the file open
SaveFile();
bool success = SaveFile();
if (!success)
{
return;
}

break;
case ContentDialogResult.Secondary:
// Don't save and continue the file open!
Original file line number Diff line number Diff line change
@@ -876,8 +876,8 @@ private bool DirtyCloseSaveFile()
_appFileName = filename;
}

SaveFile();
return true;
bool r = SaveFile();
return r;
}

/// <summary>
@@ -959,8 +959,10 @@ public void UpdateUnsavedFileIndicator(bool show)
/// <summary>
/// Wrapper method that saves the current file in place, using the current text in editor.
/// </summary>
private void SaveFile()
private bool SaveFile()
{
bool saveSuccess = true;

ChangeCursor(gridPreview, true);

// set up the FileStream for all writing
@@ -989,6 +991,8 @@ private void SaveFile()
}
catch (UnauthorizedAccessException ex)
{
saveSuccess = false;

// this exception is thrown if the file is there but marked as read only
ShowMessageBox(
resourceLoader.GetString("ErrorDialogTitle"),
@@ -997,6 +1001,8 @@ private void SaveFile()
}
catch
{
saveSuccess = false;

// this catch handles all other exceptions thrown when trying to write the file out
ShowMessageBox(
resourceLoader.GetString("ErrorDialogTitle"),
@@ -1014,6 +1020,8 @@ private void SaveFile()

// restore the cursor
ChangeCursor(gridPreview, false);

return saveSuccess;
}

/// <summary>