Skip to content

Commit 4407095

Browse files
committed
Fix auto startup
other minor cleanup Signed-off-by: Syrone Wong <wong.syrone@gmail.com>
1 parent 1a283aa commit 4407095

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

shadowsocks-csharp/Controller/System/AutoStartup.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
using System;
2+
using System.Reflection;
23
using System.Windows.Forms;
34
using Microsoft.Win32;
45
using Shadowsocks.Util;
56

67
namespace Shadowsocks.Controller
78
{
8-
class AutoStartup
9+
static class AutoStartup
910
{
10-
static string Key = "Shadowsocks_" + Application.StartupPath.GetHashCode();
11+
// Don't use Application.ExecutablePath
12+
// see https://stackoverflow.com/questions/12945805/odd-c-sharp-path-issue
13+
private static readonly string ExecutablePath = Assembly.GetEntryAssembly().Location;
14+
15+
private static string Key = "Shadowsocks_" + Application.StartupPath.GetHashCode();
1116

1217
public static bool Set(bool enabled)
1318
{
1419
RegistryKey runKey = null;
1520
try
1621
{
17-
string path = Application.ExecutablePath;
1822
runKey = Utils.OpenRegKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
1923
if ( runKey == null ) {
2024
Logging.Error( @"Cannot find HKCU\Software\Microsoft\Windows\CurrentVersion\Run" );
2125
return false;
2226
}
2327
if (enabled)
2428
{
25-
runKey.SetValue(Key, path);
29+
runKey.SetValue(Key, ExecutablePath);
2630
}
2731
else
2832
{
@@ -53,7 +57,6 @@ public static bool Check()
5357
RegistryKey runKey = null;
5458
try
5559
{
56-
string path = Application.ExecutablePath;
5760
runKey = Utils.OpenRegKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
5861
if (runKey == null) {
5962
Logging.Error(@"Cannot find HKCU\Software\Microsoft\Windows\CurrentVersion\Run");
@@ -67,10 +70,10 @@ public static bool Check()
6770
else if (item.Equals("Shadowsocks", StringComparison.OrdinalIgnoreCase)) // Compatibility with older versions
6871
{
6972
string value = Convert.ToString(runKey.GetValue(item));
70-
if (path.Equals(value, StringComparison.OrdinalIgnoreCase))
73+
if (ExecutablePath.Equals(value, StringComparison.OrdinalIgnoreCase))
7174
{
7275
runKey.DeleteValue(item);
73-
runKey.SetValue(Key, path);
76+
runKey.SetValue(Key, ExecutablePath);
7477
return true;
7578
}
7679
}

0 commit comments

Comments
 (0)