From 344c95e52c23f899f0f862e29917729d1cb0d57f Mon Sep 17 00:00:00 2001 From: ahmadali shafiee Date: Sun, 1 Nov 2020 09:44:54 +0330 Subject: [PATCH] remove duplicate startup entries fixes #3011 Signed-off-by: ahmadali shafiee --- shadowsocks-csharp/Controller/System/AutoStartup.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/shadowsocks-csharp/Controller/System/AutoStartup.cs b/shadowsocks-csharp/Controller/System/AutoStartup.cs index 97127b66..a25169d1 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) @@ -28,6 +28,17 @@ namespace Shadowsocks.Controller logger.Error(@"Cannot find HKCU\Software\Microsoft\Windows\CurrentVersion\Run"); return false; } + // Remove other startup keys with the same executable path. fixes #3011 + foreach (var valueName in runKey.GetValueNames()) + { + if (valueName == Key) + continue; + + if (string.Equals(runKey.GetValue(valueName).ToString(), Program.ExecutablePath, StringComparison.InvariantCultureIgnoreCase)) + { + runKey.DeleteValue(valueName); + } + } if (enabled) { runKey.SetValue(Key, Program.ExecutablePath);