diff --git a/shadowsocks-csharp/Controller/System/AutoStartup.cs b/shadowsocks-csharp/Controller/System/AutoStartup.cs index 97127b66..d7e92580 100644 --- a/shadowsocks-csharp/Controller/System/AutoStartup.cs +++ b/shadowsocks-csharp/Controller/System/AutoStartup.cs @@ -14,7 +14,7 @@ namespace Shadowsocks.Controller // Don't use Application.ExecutablePath // see https://stackoverflow.com/questions/12945805/odd-c-sharp-path-issue - + private static string Key = "Shadowsocks_" + Program.ExecutablePath.GetHashCode(); public static bool Set(bool enabled) @@ -71,23 +71,24 @@ namespace Shadowsocks.Controller logger.Error(@"Cannot find HKCU\Software\Microsoft\Windows\CurrentVersion\Run"); return false; } - string[] runList = runKey.GetValueNames(); - foreach (string item in runList) + var check = false; + foreach (var valueName in runKey.GetValueNames()) { - if (item.Equals(Key, StringComparison.OrdinalIgnoreCase)) - return true; - else if (item.Equals("Shadowsocks", StringComparison.OrdinalIgnoreCase)) // Compatibility with older versions + if (valueName.Equals(Key, StringComparison.InvariantCultureIgnoreCase)) { - string value = Convert.ToString(runKey.GetValue(item)); - if (Program.ExecutablePath.Equals(value, StringComparison.OrdinalIgnoreCase)) - { - runKey.DeleteValue(item); - runKey.SetValue(Key, Program.ExecutablePath); - return true; - } + check = true; + continue; + } + + // Remove other startup keys with the same executable path. fixes #3011 and also assures compatibility with older versions + if (Program.ExecutablePath.Equals(runKey.GetValue(valueName).ToString(), StringComparison.InvariantCultureIgnoreCase)) + { + runKey.DeleteValue(valueName); + runKey.SetValue(Key, Program.ExecutablePath); + check = true; } } - return false; + return check; } catch (Exception e) {