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 3.5 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
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Reflection;
  3. using System.Windows.Forms;
  4. using Microsoft.Win32;
  5. using Shadowsocks.Util;
  6. namespace Shadowsocks.Controller
  7. {
  8. static class AutoStartup
  9. {
  10. // Don't use Application.ExecutablePath
  11. // see https://stackoverflow.com/questions/12945805/odd-c-sharp-path-issue
  12. private static readonly string ExecutablePath = Assembly.GetEntryAssembly().Location;
  13. private static string Key = "Shadowsocks_" + Application.StartupPath.GetHashCode();
  14. public static bool Set(bool enabled)
  15. {
  16. RegistryKey runKey = null;
  17. try
  18. {
  19. runKey = Utils.OpenRegKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
  20. if ( runKey == null ) {
  21. Logging.Error( @"Cannot find HKCU\Software\Microsoft\Windows\CurrentVersion\Run" );
  22. return false;
  23. }
  24. if (enabled)
  25. {
  26. runKey.SetValue(Key, ExecutablePath);
  27. }
  28. else
  29. {
  30. runKey.DeleteValue(Key);
  31. }
  32. return true;
  33. }
  34. catch (Exception e)
  35. {
  36. Logging.LogUsefulException(e);
  37. return false;
  38. }
  39. finally
  40. {
  41. if (runKey != null)
  42. {
  43. try {
  44. runKey.Close();
  45. runKey.Dispose();
  46. } catch (Exception e)
  47. { Logging.LogUsefulException(e); }
  48. }
  49. }
  50. }
  51. public static bool Check()
  52. {
  53. RegistryKey runKey = null;
  54. try
  55. {
  56. runKey = Utils.OpenRegKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
  57. if (runKey == null) {
  58. Logging.Error(@"Cannot find HKCU\Software\Microsoft\Windows\CurrentVersion\Run");
  59. return false;
  60. }
  61. string[] runList = runKey.GetValueNames();
  62. foreach (string item in runList)
  63. {
  64. if (item.Equals(Key, StringComparison.OrdinalIgnoreCase))
  65. return true;
  66. else if (item.Equals("Shadowsocks", StringComparison.OrdinalIgnoreCase)) // Compatibility with older versions
  67. {
  68. string value = Convert.ToString(runKey.GetValue(item));
  69. if (ExecutablePath.Equals(value, StringComparison.OrdinalIgnoreCase))
  70. {
  71. runKey.DeleteValue(item);
  72. runKey.SetValue(Key, ExecutablePath);
  73. return true;
  74. }
  75. }
  76. }
  77. return false;
  78. }
  79. catch (Exception e)
  80. {
  81. Logging.LogUsefulException(e);
  82. return false;
  83. }
  84. finally
  85. {
  86. if (runKey != null)
  87. {
  88. try {
  89. runKey.Close();
  90. runKey.Dispose();
  91. } catch (Exception e)
  92. { Logging.LogUsefulException(e); }
  93. }
  94. }
  95. }
  96. }
  97. }