Apply Try pattern + catch exceptions
MODIFY
WalletWasabi.Fluent/ViewModels/Settings/GeneralSettingsTabViewModel.cs
WalletWasabi.Fluent/ViewModels/Settings/GeneralSettingsTabViewModel.cs
@@ -58,9 +58,11 @@ namespace WalletWasabi.Fluent.ViewModels.Settings
.ObserveOn(RxApp.TaskpoolScheduler)
.Skip(1)
.Subscribe(x =>
+ {
+ if (WalletWasabi.Helpers.StartupHelper.TryModifyStartupSetting(x))
{
Services.UiConfig.IsOsStartup = x;
- WalletWasabi.Helpers.StartupHelper.ModifyStartupSetting(x);
+ };
});
this.WhenAnyValue(x => x.CustomFee)

MODIFY
WalletWasabi/Helpers/StartupHelper.cs
WalletWasabi/Helpers/StartupHelper.cs
using Microsoft.Win32;
+using System;
using System.Reflection;
using System.Runtime.InteropServices;
+using System.Security.Permissions;
+using WalletWasabi.Logging;
namespace WalletWasabi.Helpers
{
public static class StartupHelper
{
- public static void ModifyStartupSetting(bool isWasabiStartsWithOS)
+ public static bool TryModifyStartupSetting(bool isWasabiStartsWithOS)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
- ModifyRegistry(isWasabiStartsWithOS);
+ return TryModifyRegistry(isWasabiStartsWithOS);
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
@@ -20,28 +23,48 @@ namespace WalletWasabi.Helpers
{
// Method call here
}
+ return false;
}
- private static void ModifyRegistry(bool isWasabiStartsWithOS)
+ private static bool TryModifyRegistry(bool isWasabiStartsWithOS)
+ {
+ try
{
// This extra check is only to eliminate warnings.
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
string keyName = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
- using RegistryKey key = Registry.CurrentUser.OpenSubKey(keyName, true);
+ using RegistryKey key = Registry.CurrentUser.OpenSubKey(keyName, writable: true);
if (isWasabiStartsWithOS)
{
- string pathToExe = Assembly.GetExecutingAssembly().Location;
- pathToExe = pathToExe.Remove(pathToExe.Length - 4); // This part has to change if this gets released
- pathToExe += ".Fluent.Desktop.exe";
+ string pathToExeFile = Assembly.GetExecutingAssembly().Location;
+ pathToExeFile = pathToExeFile.Remove(pathToExeFile.Length - 4); // This part has to change if this gets released
+ pathToExeFile += ".Fluent.Desktop.exe";
- key.SetValue("WasabiWallet", pathToExe);
+ key.SetValue("WasabiWallet", pathToExeFile);
}
else
{
key.DeleteValue("WasabiWallet");
}
+
+ return true;
+ }
+ }
+ catch (ArgumentNullException ex)
+ {
+ Logger.LogError(ex);
+ }
+ catch (System.Security.SecurityException ex)
+ {
+ Logger.LogError("Permission to create registry entry is denied.", ex);
}
+ catch (ObjectDisposedException ex)
+ {
+ Logger.LogError("The RegistryKey is closed or cannot be accessed.", ex);
+ }
+
+ return false;
}
}
}

Apply Try pattern + catch exceptions