Browse Source

start all instances on boot under portable model

tags/3.0
Gang Zhuo 9 years ago
parent
commit
092d1168ff
1 changed files with 37 additions and 7 deletions
  1. +37
    -7
      shadowsocks-csharp/Controller/System/AutoStartup.cs

+ 37
- 7
shadowsocks-csharp/Controller/System/AutoStartup.cs View File

@@ -6,21 +6,23 @@ namespace Shadowsocks.Controller
{ {
class AutoStartup class AutoStartup
{ {
static string Key = "Shadowsocks_" + Application.StartupPath.GetHashCode();
public static bool Set(bool enabled) public static bool Set(bool enabled)
{ {
RegistryKey runKey = null;
try try
{ {
string path = Application.ExecutablePath; string path = Application.ExecutablePath;
RegistryKey runKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
runKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
if (enabled) if (enabled)
{ {
runKey.SetValue("Shadowsocks", path);
runKey.SetValue(Key, path);
} }
else else
{ {
runKey.DeleteValue("Shadowsocks");
runKey.DeleteValue(Key);
} }
runKey.Close();
return true; return true;
} }
catch (Exception e) catch (Exception e)
@@ -28,20 +30,39 @@ namespace Shadowsocks.Controller
Logging.LogUsefulException(e); Logging.LogUsefulException(e);
return false; return false;
} }
finally
{
if (runKey != null)
{
try { runKey.Close(); }
catch (Exception e)
{ Logging.LogUsefulException(e); }
}
}
} }
public static bool Check() public static bool Check()
{ {
RegistryKey runKey = null;
try try
{ {
string path = Application.ExecutablePath; string path = Application.ExecutablePath;
RegistryKey runKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
runKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
string[] runList = runKey.GetValueNames(); string[] runList = runKey.GetValueNames();
runKey.Close();
foreach (string item in runList) foreach (string item in runList)
{ {
if (item.Equals("Shadowsocks"))
if (item.Equals(Key))
return true; return true;
else if (item.Equals("Shadowsocks")) // Compatibility with older versions
{
string value = Convert.ToString(runKey.GetValue(item));
if (path.Equals(value, StringComparison.InvariantCultureIgnoreCase))
{
runKey.DeleteValue(item);
runKey.SetValue(Key, path);
return true;
}
}
} }
return false; return false;
} }
@@ -50,6 +71,15 @@ namespace Shadowsocks.Controller
Logging.LogUsefulException(e); Logging.LogUsefulException(e);
return false; return false;
} }
finally
{
if (runKey != null)
{
try { runKey.Close(); }
catch(Exception e)
{ Logging.LogUsefulException(e); }
}
}
} }
} }
} }

Loading…
Cancel
Save