Browse Source

move code to controller/autostartup

tags/2.3
kookxiang clowwindy 10 years ago
parent
commit
3d4262d11d
3 changed files with 43 additions and 35 deletions
  1. +39
    -0
      shadowsocks-csharp/Controller/AutoStartup.cs
  2. +3
    -35
      shadowsocks-csharp/View/ConfigForm.cs
  3. +1
    -0
      shadowsocks-csharp/shadowsocks-csharp.csproj

+ 39
- 0
shadowsocks-csharp/Controller/AutoStartup.cs View File

@@ -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;
}
}
}
}

+ 3
- 35
shadowsocks-csharp/View/ConfigForm.cs View File

@@ -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();
}
}
}

+ 1
- 0
shadowsocks-csharp/shadowsocks-csharp.csproj View File

@@ -70,6 +70,7 @@
<ItemGroup>
<Compile Include="3rd\QRCodeCS.cs" />
<Compile Include="3rd\SimpleJson.cs" />
<Compile Include="Controller\AutoStartup.cs" />
<Compile Include="Controller\FileManager.cs" />
<Compile Include="Controller\Logging.cs" />
<Compile Include="Controller\UpdateChecker.cs" />


Loading…
Cancel
Save