diff --git a/Greenshot/Forms/SettingsForm.cs b/Greenshot/Forms/SettingsForm.cs index 7bf5a17b5..87ac1a8b5 100644 --- a/Greenshot/Forms/SettingsForm.cs +++ b/Greenshot/Forms/SettingsForm.cs @@ -352,14 +352,16 @@ namespace Greenshot { numericUpDownWaitTime.Value = coreConfiguration.CaptureDelay >=0?coreConfiguration.CaptureDelay:0; - // If the run for all is set we disable and set the checkbox - if (StartupHelper.checkRunAll()) { - checkbox_autostartshortcut.Enabled = false; - checkbox_autostartshortcut.Checked = true; + // Autostart checkbox logic. + if (StartupHelper.hasRunAll()) { + // Remove runUser if we already have a run under all + StartupHelper.deleteRunUser(); + checkbox_autostartshortcut.Enabled = StartupHelper.canWriteRunAll(); + checkbox_autostartshortcut.Checked = StartupHelper.hasRunAll(); } else { // No run for all, enable the checkbox and set it to true if the current user has a key - checkbox_autostartshortcut.Enabled = true; - checkbox_autostartshortcut.Checked = StartupHelper.checkRunUser(); + checkbox_autostartshortcut.Enabled = StartupHelper.canWriteRunUser(); + checkbox_autostartshortcut.Checked = StartupHelper.hasRunUser(); } numericUpDown_daysbetweencheck.Value = coreConfiguration.UpdateCheckInterval; @@ -408,17 +410,20 @@ namespace Greenshot { coreConfiguration.UpdateCheckInterval = (int)numericUpDown_daysbetweencheck.Value; try { - // Check if the Run for all is set - if(!StartupHelper.checkRunAll()) { - // If not set the registry according to the settings - if (checkbox_autostartshortcut.Checked) { + if (checkbox_autostartshortcut.Checked) { + // It's checked, so we set the RunUser if the RunAll isn't set. + // Do this every time, so the executable is correct. + if (!StartupHelper.hasRunAll()) { StartupHelper.setRunUser(); - } else { - StartupHelper.deleteRunUser(); } } else { - // The run key for Greenshot is set for all users, delete the local version! - StartupHelper.deleteRunUser(); + // Delete both settings if it's unchecked + if (StartupHelper.hasRunAll()) { + StartupHelper.deleteRunAll(); + } + if (StartupHelper.hasRunUser()) { + StartupHelper.deleteRunUser(); + } } } catch (Exception e) { LOG.Warn("Problem checking registry, ignoring for now: ", e); diff --git a/Greenshot/Helpers/StartupHelper.cs b/Greenshot/Helpers/StartupHelper.cs index 1ef5f02b4..5ce3d1889 100644 --- a/Greenshot/Helpers/StartupHelper.cs +++ b/Greenshot/Helpers/StartupHelper.cs @@ -29,20 +29,53 @@ namespace Greenshot.Helpers { public static class StartupHelper { private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(StartupHelper)); + private const string RUNKEY6432 = @"Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Run"; private const string RUNKEY = @"Software\Microsoft\Windows\CurrentVersion\Run"; - private const string LOCALMACHINE = @"HKEY_LOCAL_MACHINE\"; - private const string CURRENTUSER = @"HKEY_CURRENT_USER\"; private const string APPLICATIONNAME = "Greenshot"; private static string getExecutablePath() { return "\"" + Application.ExecutablePath + "\""; } - + + public static bool canWriteRunAll() { + try { + using (RegistryKey key = Registry.LocalMachine.OpenSubKey(RUNKEY, true)) { + } + } catch { + return false; + } + return true; + } + + public static bool canWriteRunUser() { + try { + using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY, true)) { + } + } catch { + return false; + } + return true; + } + public static Object getRunAllValue() { using (RegistryKey key = Registry.LocalMachine.OpenSubKey(RUNKEY, false)) { if (key != null) { - return key.GetValue(APPLICATIONNAME); + object runValue = key.GetValue(APPLICATIONNAME); + if (runValue != null) { + return runValue; + } + } + } + // for 64-bit systems we need to check the 32-bit keys too + if (IntPtr.Size == 8) { + using (RegistryKey key = Registry.LocalMachine.OpenSubKey(RUNKEY6432, false)) { + if (key != null) { + object runValue = key.GetValue(APPLICATIONNAME); + if (runValue != null) { + return runValue; + } + } } } return null; @@ -51,23 +84,36 @@ namespace Greenshot.Helpers { public static Object getRunUserValue() { using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY, false)) { if (key != null) { - return key.GetValue(APPLICATIONNAME); + object runValue = key.GetValue(APPLICATIONNAME); + if (runValue != null) { + return runValue; + } + } + } + // for 64-bit systems we need to check the 32-bit keys too + if (IntPtr.Size == 8) { + using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY6432, false)) { + if (key != null) { + object runValue = key.GetValue(APPLICATIONNAME); + if (runValue != null) { + return runValue; + } + } } } return null; } - public static bool checkRunAll() { - Object runValue = null; + public static bool hasRunAll() { try { - runValue = getRunAllValue(); + return getRunAllValue() != null; } catch (Exception e) { LOG.Error("Error retrieving RunAllValue", e); } - return runValue != null; + return false; } - public static bool checkRunUser() { + public static bool hasRunUser() { Object runValue = null; try { runValue = getRunUserValue(); @@ -76,13 +122,45 @@ namespace Greenshot.Helpers { } return runValue != null; } - - public static void deleteRunUser() { - using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY, true)) { + + public static void deleteRunAll() { + if (hasRunAll()) { try { - if (checkRunUser()) { + using (RegistryKey key = Registry.LocalMachine.OpenSubKey(RUNKEY, true)) { key.DeleteValue(APPLICATIONNAME); } + } catch (Exception e) { + LOG.Error("Error in deleteRunAll.", e); + } + try { + // for 64-bit systems we need to delete the 32-bit keys too + if (IntPtr.Size == 8) { + using (RegistryKey key = Registry.LocalMachine.OpenSubKey(RUNKEY6432, false)) { + key.DeleteValue(APPLICATIONNAME); + } + } + } catch (Exception e) { + LOG.Error("Error in deleteRunAll.", e); + } + } + } + + public static void deleteRunUser() { + if (hasRunUser()) { + try { + using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY, true)) { + key.DeleteValue(APPLICATIONNAME); + } + } catch (Exception e) { + LOG.Error("Error in deleteRunUser.", e); + } + try { + // for 64-bit systems we need to delete the 32-bit keys too + if (IntPtr.Size == 8) { + using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY6432, false)) { + key.DeleteValue(APPLICATIONNAME); + } + } } catch (Exception e) { LOG.Error("Error in deleteRunUser.", e); } @@ -90,12 +168,12 @@ namespace Greenshot.Helpers { } public static void setRunUser() { - using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY, true)) { - try { + try { + using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY, true)) { key.SetValue(APPLICATIONNAME, getExecutablePath()); - } catch (Exception e) { - LOG.Error("Error in setRunUser.", e); } + } catch (Exception e) { + LOG.Error("Error in setRunUser.", e); } } }