From c285926393576dc8210c6f4cb327c01d43f22f74 Mon Sep 17 00:00:00 2001 From: kookxiang Date: Wed, 12 Nov 2014 14:14:03 +0800 Subject: [PATCH] move code to controller/autostartup --- shadowsocks-csharp/Controller/AutoStartup.cs | 39 ++++++++++++++++++++++++++++ shadowsocks-csharp/View/ConfigForm.cs | 38 +++------------------------ shadowsocks-csharp/shadowsocks-csharp.csproj | 1 + 3 files changed, 43 insertions(+), 35 deletions(-) create mode 100644 shadowsocks-csharp/Controller/AutoStartup.cs diff --git a/shadowsocks-csharp/Controller/AutoStartup.cs b/shadowsocks-csharp/Controller/AutoStartup.cs new file mode 100644 index 00000000..cc7e0b2b --- /dev/null +++ b/shadowsocks-csharp/Controller/AutoStartup.cs @@ -0,0 +1,39 @@ +using System; +using System.Windows.Forms; +using Microsoft.Win32; + +namespace Shadowsocks.Controller { + class AutoStartup { + public static bool Set(bool enabled) { + try { + string path = Application.ExecutablePath; + RegistryKey runKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); + if (enabled) { + runKey.SetValue("Shadowsocks", path); + } else { + runKey.DeleteValue("Shadowsocks"); + } + runKey.Close(); + return true; + } catch (Exception) { + return false; + } + } + + public static bool Check() { + try { + string path = Application.ExecutablePath; + RegistryKey runKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); + string[] runList = runKey.GetValueNames(); + runKey.Close(); + foreach (string item in runList) { + if (item.Equals("Shadowsocks")) + return true; + } + return false; + } catch (Exception) { + return false; + } + } + } +} diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 74cac77a..21d17487 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -379,47 +379,15 @@ namespace Shadowsocks.View qrCodeForm.Show(); } - private bool setAutoStartup(bool enabled) { - try { - string path = Application.ExecutablePath; - RegistryKey runKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); - if (enabled) { - runKey.SetValue("Shadowsocks-CSharp", path); - } else { - runKey.DeleteValue("Shadowsocks-CSharp"); - } - runKey.Close(); - return true; - } catch (Exception e) { - return false; - } - } - - private bool checkAutoStartup() { - try { - string path = Application.ExecutablePath; - RegistryKey runKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); - string[] runList = runKey.GetValueNames(); - runKey.Close(); - foreach(string item in runList){ - if (item.Equals("Shadowsocks-CSharp")) - return true; - } - return false; - } catch (Exception e) { - return false; - } - } - private void autoStartup_Click(object sender, EventArgs e) { autoStartup.Checked = !autoStartup.Checked; - if (!setAutoStartup(autoStartup.Checked)) { - //MessageBox.Show("Failed to edit registry"); + if (!AutoStartup.Set(autoStartup.Checked)) { + MessageBox.Show("Failed to edit registry"); } } private void contextMenu1_Popup(object sender, EventArgs e) { - autoStartup.Checked = checkAutoStartup(); + autoStartup.Checked = AutoStartup.Check(); } } } diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index d081645c..533186eb 100755 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -70,6 +70,7 @@ +