86 lines
2.7 KiB
C#
86 lines
2.7 KiB
C#
using System;
|
|
using System.Windows.Forms;
|
|
using Microsoft.Win32;
|
|
|
|
namespace Shadowsocks.Controller
|
|
{
|
|
class AutoStartup
|
|
{
|
|
static string Key = "Shadowsocks_" + Application.StartupPath.GetHashCode();
|
|
|
|
public static bool Set(bool enabled)
|
|
{
|
|
RegistryKey runKey = null;
|
|
try
|
|
{
|
|
string path = Application.ExecutablePath;
|
|
runKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
|
|
if (enabled)
|
|
{
|
|
runKey.SetValue(Key, path);
|
|
}
|
|
else
|
|
{
|
|
runKey.DeleteValue(Key);
|
|
}
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Logging.LogUsefulException(e);
|
|
return false;
|
|
}
|
|
finally
|
|
{
|
|
if (runKey != null)
|
|
{
|
|
try { runKey.Close(); }
|
|
catch (Exception e)
|
|
{ Logging.LogUsefulException(e); }
|
|
}
|
|
}
|
|
}
|
|
|
|
public static bool Check()
|
|
{
|
|
RegistryKey runKey = null;
|
|
try
|
|
{
|
|
string path = Application.ExecutablePath;
|
|
runKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
|
|
string[] runList = runKey.GetValueNames();
|
|
foreach (string item in runList)
|
|
{
|
|
if (item.Equals(Key, StringComparison.OrdinalIgnoreCase))
|
|
return true;
|
|
else if (item.Equals("Shadowsocks", StringComparison.OrdinalIgnoreCase)) // Compatibility with older versions
|
|
{
|
|
string value = Convert.ToString(runKey.GetValue(item));
|
|
if (path.Equals(value, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
runKey.DeleteValue(item);
|
|
runKey.SetValue(Key, path);
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Logging.LogUsefulException(e);
|
|
return false;
|
|
}
|
|
finally
|
|
{
|
|
if (runKey != null)
|
|
{
|
|
try { runKey.Close(); }
|
|
catch (Exception e)
|
|
{ Logging.LogUsefulException(e); }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|