You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

AutoStartup.cs 2.8 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Windows.Forms;
  3. using Microsoft.Win32;
  4. namespace Shadowsocks.Controller
  5. {
  6. class AutoStartup
  7. {
  8. static string Key = "Shadowsocks_" + Application.StartupPath.GetHashCode();
  9. public static bool Set(bool enabled)
  10. {
  11. RegistryKey runKey = null;
  12. try
  13. {
  14. string path = Application.ExecutablePath;
  15. runKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
  16. if (enabled)
  17. {
  18. runKey.SetValue(Key, path);
  19. }
  20. else
  21. {
  22. runKey.DeleteValue(Key);
  23. }
  24. return true;
  25. }
  26. catch (Exception e)
  27. {
  28. Logging.LogUsefulException(e);
  29. return false;
  30. }
  31. finally
  32. {
  33. if (runKey != null)
  34. {
  35. try { runKey.Close(); }
  36. catch (Exception e)
  37. { Logging.LogUsefulException(e); }
  38. }
  39. }
  40. }
  41. public static bool Check()
  42. {
  43. RegistryKey runKey = null;
  44. try
  45. {
  46. string path = Application.ExecutablePath;
  47. runKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
  48. string[] runList = runKey.GetValueNames();
  49. foreach (string item in runList)
  50. {
  51. if (item.Equals(Key, StringComparison.OrdinalIgnoreCase))
  52. return true;
  53. else if (item.Equals("Shadowsocks", StringComparison.OrdinalIgnoreCase)) // Compatibility with older versions
  54. {
  55. string value = Convert.ToString(runKey.GetValue(item));
  56. if (path.Equals(value, StringComparison.OrdinalIgnoreCase))
  57. {
  58. runKey.DeleteValue(item);
  59. runKey.SetValue(Key, path);
  60. return true;
  61. }
  62. }
  63. }
  64. return false;
  65. }
  66. catch (Exception e)
  67. {
  68. Logging.LogUsefulException(e);
  69. return false;
  70. }
  71. finally
  72. {
  73. if (runKey != null)
  74. {
  75. try { runKey.Close(); }
  76. catch (Exception e)
  77. { Logging.LogUsefulException(e); }
  78. }
  79. }
  80. }
  81. }
  82. }