From df2ec89dbb9fda142aea4a16309f406094c85b01 Mon Sep 17 00:00:00 2001 From: Patrick Demichiel Date: Thu, 3 Oct 2024 06:45:47 +0200 Subject: [PATCH] Add error handling for LastSwitchedUsername setting Added a using directive for FastAccountSwitcher.CLI.Services in MainViewModel.cs. Implemented a try-catch block in MainViewModel to handle exceptions when accessing Settings.Default.LastSwitchedUsername, logging errors and resetting settings if necessary. Removed unused using directive for System.Windows.Threading in MainWindow.xaml.cs. --- src/FastAccountSwitcher.GUI/MainViewModel.cs | 28 ++++++++++++++++++- .../MainWindow.xaml.cs | 1 - 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/FastAccountSwitcher.GUI/MainViewModel.cs b/src/FastAccountSwitcher.GUI/MainViewModel.cs index b66b367..7726d93 100644 --- a/src/FastAccountSwitcher.GUI/MainViewModel.cs +++ b/src/FastAccountSwitcher.GUI/MainViewModel.cs @@ -1,5 +1,6 @@ using System.Windows.Controls; +using FastAccountSwitcher.CLI.Services; using FastAccountSwitcher.GUI.Properties; namespace FastAccountSwitcher.GUI; @@ -90,7 +91,32 @@ public void UltraFastSwitch() return Accounts[0]; } - var lastSwitchedUsername = Settings.Default.LastSwitchedUsername; + string? lastSwitchedUsername = null; + try + { + lastSwitchedUsername = Settings.Default.LastSwitchedUsername; + } + catch (Exception ex) + { + LoggingService.LogException(ex); + LoggingService.LogInfo("Resetting settings"); + Settings.Default.Reset(); + Settings.Default.Save(); + + // probably need to delete "%localappdata%\Fast_Account_Switcher" folder? + + try + { + lastSwitchedUsername = Settings.Default.LastSwitchedUsername; + } + catch (Exception ex2) + { + LoggingService.LogException(ex2); + LoggingService.LogInfo("Resetting settings failed"); + return null; + } + } + if (string.IsNullOrEmpty(lastSwitchedUsername)) { return null; diff --git a/src/FastAccountSwitcher.GUI/MainWindow.xaml.cs b/src/FastAccountSwitcher.GUI/MainWindow.xaml.cs index f802ccc..c8acd08 100644 --- a/src/FastAccountSwitcher.GUI/MainWindow.xaml.cs +++ b/src/FastAccountSwitcher.GUI/MainWindow.xaml.cs @@ -1,6 +1,5 @@ using System.ComponentModel; using System.Windows.Controls; -using System.Windows.Threading; using Hardcodet.Wpf.TaskbarNotification;