From 7f0a9bf1c8ccda6daa0c8ce7995c4e1cc8364844 Mon Sep 17 00:00:00 2001 From: thuannd88 Date: Mon, 9 Sep 2024 10:58:44 +0700 Subject: [PATCH] add try catch load settings --- Admin/AdminSettingsUC.xaml.cs | 63 ++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/Admin/AdminSettingsUC.xaml.cs b/Admin/AdminSettingsUC.xaml.cs index 76a60e9..013f31a 100644 --- a/Admin/AdminSettingsUC.xaml.cs +++ b/Admin/AdminSettingsUC.xaml.cs @@ -31,24 +31,39 @@ namespace SafeBagDE70.Admin public AdminSettingsUC() { InitializeComponent(); - LoadStartupSetting(); - timePicker.SelectedTime = Properties.Settings.Default.RestartTime; - SetupAutoRestart(); + try + { + LoadStartupSetting(); + timePicker.SelectedTime = Properties.Settings.Default.RestartTime; + SetupAutoRestart(); + } + catch (Exception ex) { + Log.Fatal(ex, "AdminSettingsUC"); + } + } private void LoadStartupSetting() { - // Open the registry key for the current user's startup applications - RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", false); - appName = Assembly.GetEntryAssembly()?.GetName().Name; - if (key != null) + try { - // Check if the app is already set to start with Windows - string value = key.GetValue(appName) as string; + // Open the registry key for the current user's startup applications + RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", false); + appName = Assembly.GetEntryAssembly()?.GetName().Name; + if (key != null) + { + // Check if the app is already set to start with Windows + string value = key.GetValue(appName) as string; - // If the app is listed in the startup items, set the checkbox to checked - cbStartWithWindows.IsChecked = value != null; + // If the app is listed in the startup items, set the checkbox to checked + cbStartWithWindows.IsChecked = value != null; + } } + catch (Exception ex) + { + Log.Fatal(ex, "LoadStartupSetting"); + } + } private void cbStartWithWindows_Checked(object sender, RoutedEventArgs e) { @@ -64,17 +79,25 @@ namespace SafeBagDE70.Admin private void AddApplicationToStartup() { - // Get the path to the executable - string exePath = Process.GetCurrentProcess().MainModule.FileName; - - // Open the registry key for the current user's startup applications - RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); - - if (key != null) + try { - // Add the application to the startup list - key.SetValue(appName, exePath); + // Get the path to the executable + string exePath = Process.GetCurrentProcess().MainModule.FileName; + + // Open the registry key for the current user's startup applications + RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); + + if (key != null) + { + // Add the application to the startup list + key.SetValue(appName, exePath); + } } + catch (Exception ex) + { + Log.Fatal(ex, "AddApplicationToStartup"); + } + } private void RemoveApplicationFromStartup()