Skip to content

Commit

Permalink
fix: handle unauthorized access errors for settings folder
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Jun 16, 2023
1 parent e379de0 commit 0c949f3
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions MidiShapeShifter/MidiShapeShifter/Mss/MssFileSystemLocations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,36 @@ namespace MidiShapeShifter.Mss
{
public static class MssFileSystemLocations
{

public static string default_settings_folder()
{
// set it to the current user's Documents/MidiShapeShifter folder
string current_user_documents_folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
Debug.Assert(current_user_documents_folder != "");
return current_user_documents_folder + @"\" + MssConstants.APP_FOLDER_NAME + @"\";
}

public static string SettingsFolder
{
get
{
RegistryKey mssLocalMachineRegKey = CreateMssCurrentUserRegKey();
var settingsFolderObject = mssLocalMachineRegKey.GetValue("SettingsFolder");
Debug.Assert(mssLocalMachineRegKey != null);
object settingsFolderObject = mssLocalMachineRegKey.GetValue("SettingsFolder");

// if doesn't exist, create it
string settingsFolder = "";
if (settingsFolderObject == null)
{
// set it to the current user's Documents/MidiShapeShifter folder
string current_user_documents_folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
Debug.Assert(current_user_documents_folder != "");
string default_settings_folder = current_user_documents_folder + @"\" + MssConstants.APP_FOLDER_NAME + @"\";

mssLocalMachineRegKey.SetValue("SettingsFolder", default_settings_folder);
settingsFolder = default_settings_folder;
settingsFolder = default_settings_folder();

try
{
mssLocalMachineRegKey.SetValue("SettingsFolder", settingsFolder, RegistryValueKind.ExpandString);
}
catch (System.Exception err)
{
Logger.Warning(0, "Unable to create registry key for SettingsFolder. Try again as administrator or ignore the warning:" + err.Message);
}
}
Debug.Assert(settingsFolder != "");

Expand All @@ -48,9 +58,9 @@ public static string SettingsFolder

private static RegistryKey CreateMssCurrentUserRegKey()
{
RegistryKey localMachine32 = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser, RegistryView.Registry32);
return localMachine32.OpenSubKey(@"SOFTWARE\" + MssConstants.APP_NAME) ??
localMachine32.CreateSubKey(@"SOFTWARE\" + MssConstants.APP_NAME);
RegistryKey current_user = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser, RegistryView.Registry32);
return current_user.OpenSubKey(@"SOFTWARE\" + MssConstants.APP_NAME) ??
current_user.CreateSubKey(@"SOFTWARE\" + MssConstants.APP_NAME);
}
}

Expand Down

0 comments on commit 0c949f3

Please sign in to comment.