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 5.7 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
7 years ago
7 years ago
7 years ago
7 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Runtime.InteropServices;
  6. using System.Windows.Forms;
  7. using Microsoft.Win32;
  8. using Shadowsocks.Util;
  9. namespace Shadowsocks.Controller
  10. {
  11. static class AutoStartup
  12. {
  13. // Don't use Application.ExecutablePath
  14. // see https://stackoverflow.com/questions/12945805/odd-c-sharp-path-issue
  15. private static readonly string ExecutablePath = Assembly.GetEntryAssembly().Location;
  16. private static string Key = "Shadowsocks_" + Application.StartupPath.GetHashCode();
  17. public static bool Set(bool enabled)
  18. {
  19. RegistryKey runKey = null;
  20. try
  21. {
  22. runKey = Utils.OpenRegKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
  23. if (runKey == null)
  24. {
  25. Logging.Error(@"Cannot find HKCU\Software\Microsoft\Windows\CurrentVersion\Run");
  26. return false;
  27. }
  28. if (enabled)
  29. {
  30. runKey.SetValue(Key, ExecutablePath);
  31. }
  32. else
  33. {
  34. runKey.DeleteValue(Key);
  35. }
  36. // When autostartup setting change, change RegisterForRestart state to avoid start 2 times
  37. RegisterForRestart(!enabled);
  38. return true;
  39. }
  40. catch (Exception e)
  41. {
  42. Logging.LogUsefulException(e);
  43. return false;
  44. }
  45. finally
  46. {
  47. if (runKey != null)
  48. {
  49. try
  50. {
  51. runKey.Close();
  52. runKey.Dispose();
  53. }
  54. catch (Exception e)
  55. { Logging.LogUsefulException(e); }
  56. }
  57. }
  58. }
  59. public static bool Check()
  60. {
  61. RegistryKey runKey = null;
  62. try
  63. {
  64. runKey = Utils.OpenRegKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
  65. if (runKey == null)
  66. {
  67. Logging.Error(@"Cannot find HKCU\Software\Microsoft\Windows\CurrentVersion\Run");
  68. return false;
  69. }
  70. string[] runList = runKey.GetValueNames();
  71. foreach (string item in runList)
  72. {
  73. if (item.Equals(Key, StringComparison.OrdinalIgnoreCase))
  74. return true;
  75. else if (item.Equals("Shadowsocks", StringComparison.OrdinalIgnoreCase)) // Compatibility with older versions
  76. {
  77. string value = Convert.ToString(runKey.GetValue(item));
  78. if (ExecutablePath.Equals(value, StringComparison.OrdinalIgnoreCase))
  79. {
  80. runKey.DeleteValue(item);
  81. runKey.SetValue(Key, ExecutablePath);
  82. return true;
  83. }
  84. }
  85. }
  86. return false;
  87. }
  88. catch (Exception e)
  89. {
  90. Logging.LogUsefulException(e);
  91. return false;
  92. }
  93. finally
  94. {
  95. if (runKey != null)
  96. {
  97. try
  98. {
  99. runKey.Close();
  100. runKey.Dispose();
  101. }
  102. catch (Exception e)
  103. { Logging.LogUsefulException(e); }
  104. }
  105. }
  106. }
  107. [DllImport("kernel32.dll", SetLastError = true)]
  108. static extern int RegisterApplicationRestart([MarshalAs(UnmanagedType.LPWStr)] string commandLineArgs, int Flags);
  109. [DllImport("kernel32.dll", SetLastError = true)]
  110. static extern int UnregisterApplicationRestart();
  111. [Flags]
  112. enum ApplicationRestartFlags
  113. {
  114. RESTART_ALWAYS = 0,
  115. RESTART_NO_CRASH = 1,
  116. RESTART_NO_HANG = 2,
  117. RESTART_NO_PATCH = 4,
  118. RESTART_NO_REBOOT = 8,
  119. }
  120. // register restart after system reboot/update
  121. public static void RegisterForRestart(bool register)
  122. {
  123. // requested register and not autostartup
  124. if (register && !Check())
  125. {
  126. // escape command line parameter
  127. string[] args = new List<string>(Program.Args)
  128. .Select(p => p.Replace("\"", "\\\"")) // escape " to \"
  129. .Select(p => p.IndexOf(" ") >= 0 ? "\"" + p + "\"" : p) // encapsule with "
  130. .ToArray();
  131. string cmdline = string.Join(" ", args);
  132. // first parameter is process command line parameter
  133. // needn't include the name of the executable in the command line
  134. RegisterApplicationRestart(cmdline, (int)(ApplicationRestartFlags.RESTART_NO_CRASH | ApplicationRestartFlags.RESTART_NO_HANG));
  135. Logging.Debug("Register restart after system reboot, command line:" + cmdline);
  136. }
  137. // requested unregister, which has no side effect
  138. else if (!register)
  139. {
  140. UnregisterApplicationRestart();
  141. Logging.Debug("Unregister restart after system reboot");
  142. }
  143. }
  144. }
  145. }