NTMinerRegistry.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using Microsoft.Win32;
  2. using System;
  3. namespace NTMiner {
  4. public static partial class NTMinerRegistry {
  5. #region 设置Windows开机启动
  6. /// <summary>
  7. /// 将当前程序设置为windows开机自动启动
  8. /// </summary>
  9. /// <param name="valueName"></param>
  10. /// <param name="isAutoBoot"></param>
  11. /// <param name="otherParams"></param>
  12. public static void SetAutoBoot(string valueName, bool isAutoBoot, string otherParams = null) {
  13. const string AutoRunSubKey = @"Software\Microsoft\Windows\CurrentVersion\Run";
  14. if (isAutoBoot == true) {
  15. string value = VirtualRoot.AppFileFullName;
  16. if (!string.IsNullOrEmpty(otherParams)) {
  17. value = value + " " + otherParams;
  18. }
  19. Windows.WinRegistry.SetValue(Registry.CurrentUser, AutoRunSubKey, valueName, value);
  20. }
  21. else {
  22. Windows.WinRegistry.DeleteValue(Registry.CurrentUser, AutoRunSubKey, valueName);
  23. }
  24. }
  25. #endregion
  26. public const string NTMinerRegistrySubKey = @".DEFAULT\Software\NTMiner";
  27. // 下面这些项是可能需要交换到下层系统从而完成不同进程间信息交换的项
  28. // 注册表就属于下层系统,文件系统也属于下层系统,使用注册表比较简单统一
  29. private const string MinerStudio = "MinerStudio";
  30. private static string GetValueName(NTMinerAppType appType, string baseValueName) {
  31. string valueName;
  32. switch (appType) {
  33. case NTMinerAppType.MinerClient:
  34. valueName = baseValueName;
  35. break;
  36. case NTMinerAppType.MinerStudio:
  37. valueName = MinerStudio + baseValueName;
  38. break;
  39. default:
  40. throw new InvalidProgramException();
  41. }
  42. return valueName;
  43. }
  44. #region Location
  45. public static string GetLocation(NTMinerAppType appType) {
  46. string valueName = GetValueName(appType, NTKeyword.LocationRegistryKey);
  47. object value = Windows.WinRegistry.GetValue(Registry.Users, NTMinerRegistrySubKey, valueName);
  48. if (value != null) {
  49. return (string)value;
  50. }
  51. return string.Empty;
  52. }
  53. public static void SetLocation(NTMinerAppType appType, string location) {
  54. string valueName = GetValueName(appType, NTKeyword.LocationRegistryKey);
  55. Windows.WinRegistry.SetValue(Registry.Users, NTMinerRegistrySubKey, valueName, location);
  56. }
  57. #endregion
  58. #region Arguments
  59. public static string GetArguments(NTMinerAppType appType) {
  60. string valueName = GetValueName(appType, NTKeyword.ArgumentsRegistryKey);
  61. object value = Windows.WinRegistry.GetValue(Registry.Users, NTMinerRegistrySubKey, valueName);
  62. if (value != null) {
  63. return (string)value;
  64. }
  65. return string.Empty;
  66. }
  67. public static void SetArguments(NTMinerAppType appType, string arguments) {
  68. string valueName = GetValueName(appType, NTKeyword.ArgumentsRegistryKey);
  69. Windows.WinRegistry.SetValue(Registry.Users, NTMinerRegistrySubKey, valueName, arguments);
  70. }
  71. #endregion
  72. #region CurrentVersion
  73. public static string GetCurrentVersion(NTMinerAppType appType) {
  74. string valueName = GetValueName(appType, NTKeyword.CurrentVersionRegistryKey);
  75. string currentVersion = "1.0.0.0";
  76. object value = Windows.WinRegistry.GetValue(Registry.Users, NTMinerRegistrySubKey, valueName);
  77. if (value != null) {
  78. currentVersion = (string)value;
  79. }
  80. if (string.IsNullOrEmpty(currentVersion)) {
  81. return "1.0.0.0";
  82. }
  83. return currentVersion;
  84. }
  85. public static void SetCurrentVersion(NTMinerAppType appType, string version) {
  86. string valueName = GetValueName(appType, NTKeyword.CurrentVersionRegistryKey);
  87. Windows.WinRegistry.SetValue(Registry.Users, NTMinerRegistrySubKey, valueName, version);
  88. }
  89. #endregion
  90. #region CurrentVersionTag
  91. public static string GetCurrentVersionTag(NTMinerAppType appType) {
  92. string valueName = GetValueName(appType, NTKeyword.CurrentVersionTagRegistryKey);
  93. string currentVersionTag = string.Empty;
  94. object value = Windows.WinRegistry.GetValue(Registry.Users, NTMinerRegistrySubKey, valueName);
  95. if (value != null) {
  96. currentVersionTag = (string)value;
  97. }
  98. return currentVersionTag;
  99. }
  100. public static void SetCurrentVersionTag(NTMinerAppType appType, string versionTag) {
  101. string valueName = GetValueName(appType, NTKeyword.CurrentVersionTagRegistryKey);
  102. Windows.WinRegistry.SetValue(Registry.Users, NTMinerRegistrySubKey, valueName, versionTag);
  103. }
  104. #endregion
  105. #region MinerStudioIsInnerIp
  106. public static bool GetMinerStudioIsInnerIp() {
  107. object value = Windows.WinRegistry.GetValue(Registry.Users, NTMinerRegistrySubKey, "MinerStudioIsInnerIp");
  108. return value != null && value.ToString() == "true";
  109. }
  110. public static void SetMinerStudioIsInnerIp(bool isInnerIp) {
  111. Windows.WinRegistry.SetValue(Registry.Users, NTMinerRegistrySubKey, "MinerStudioIsInnerIp", isInnerIp ? "true" : "false");
  112. }
  113. #endregion
  114. #region IsNoUi
  115. public static bool GetIsNoUi() {
  116. object value = Windows.WinRegistry.GetValue(Registry.Users, NTMinerRegistrySubKey, "IsNoUi");
  117. return value != null && value.ToString() == "true";
  118. }
  119. public static void SetIsNoUi(bool isNoUi) {
  120. Windows.WinRegistry.SetValue(Registry.Users, NTMinerRegistrySubKey, "IsNoUi", isNoUi ? "true" : "false");
  121. }
  122. #endregion
  123. #region IsAutoStart
  124. public static bool GetIsAutoStart() {
  125. object value = Windows.WinRegistry.GetValue(Registry.Users, NTMinerRegistrySubKey, "IsAutoStart");
  126. return value != null && value.ToString() == "true";
  127. }
  128. public static void SetIsAutoStart(bool isAutoStart) {
  129. Windows.WinRegistry.SetValue(Registry.Users, NTMinerRegistrySubKey, "IsAutoStart", isAutoStart ? "true" : "false");
  130. }
  131. #endregion
  132. #region LoginName
  133. public static string GetLoginName() {
  134. object value = Windows.WinRegistry.GetValue(Registry.Users, NTMinerRegistrySubKey, NTKeyword.LoginNameRegistryKey);
  135. if (value == null) {
  136. value = Windows.WinRegistry.GetValue(Registry.Users, NTMinerRegistrySubKey, "ControlCenterLoginName");
  137. }
  138. if (value == null) {
  139. return string.Empty;
  140. }
  141. return (string)value;
  142. }
  143. public static void SetLoginName(string daemonVersion) {
  144. if (daemonVersion == null) {
  145. daemonVersion = string.Empty;
  146. }
  147. Windows.WinRegistry.SetValue(Registry.Users, NTMinerRegistrySubKey, NTKeyword.LoginNameRegistryKey, daemonVersion);
  148. }
  149. #endregion
  150. #region NoDevFeeVersion
  151. public static string GetNoDevFeeVersion() {
  152. object value = Windows.WinRegistry.GetValue(Registry.Users, NTMinerRegistrySubKey, NTKeyword.NoDevFeeVersionRegistryKey);
  153. if (value == null) {
  154. return string.Empty;
  155. }
  156. return (string)value;
  157. }
  158. public static void SetNoDevFeeVersion(string noDevFeeVersion) {
  159. if (noDevFeeVersion == null) {
  160. noDevFeeVersion = string.Empty;
  161. }
  162. Windows.WinRegistry.SetValue(Registry.Users, NTMinerRegistrySubKey, NTKeyword.NoDevFeeVersionRegistryKey, noDevFeeVersion);
  163. }
  164. #endregion
  165. #region DaemonVersion
  166. public static string GetDaemonVersion() {
  167. object value = Windows.WinRegistry.GetValue(Registry.Users, NTMinerRegistrySubKey, NTKeyword.DaemonVersionRegistryKey);
  168. if (value == null) {
  169. return string.Empty;
  170. }
  171. return (string)value;
  172. }
  173. public static void SetDaemonVersion(string daemonVersion) {
  174. if (daemonVersion == null) {
  175. daemonVersion = string.Empty;
  176. }
  177. Windows.WinRegistry.SetValue(Registry.Users, NTMinerRegistrySubKey, NTKeyword.DaemonVersionRegistryKey, daemonVersion);
  178. }
  179. #endregion
  180. #region GetClientId
  181. public static Guid GetClientId(NTMinerAppType appType) {
  182. string valueName = GetValueName(appType, NTKeyword.ClientIdRegistryKey);
  183. object value = Windows.WinRegistry.GetValue(Registry.Users, NTMinerRegistrySubKey, valueName);
  184. if (value == null || !Guid.TryParse((string)value, out Guid id)) {
  185. id = Guid.NewGuid();
  186. Windows.WinRegistry.SetValue(Registry.Users, NTMinerRegistrySubKey, valueName, id.ToString());
  187. }
  188. return id;
  189. }
  190. #endregion
  191. #region ReClientId
  192. public static void ReClientId(NTMinerAppType appType) {
  193. string valueName = GetValueName(appType, NTKeyword.ClientIdRegistryKey);
  194. Windows.WinRegistry.DeleteValue(Registry.Users, NTMinerRegistrySubKey, valueName);
  195. }
  196. #endregion
  197. #region GetIsRdpEnabled
  198. public static bool GetIsRdpEnabled() {
  199. try {
  200. return (int)Windows.WinRegistry.GetValue(Registry.LocalMachine, "SYSTEM\\CurrentControlSet\\Control\\Terminal Server", "fDenyTSConnections") == 0;
  201. }
  202. catch {
  203. return false;
  204. }
  205. }
  206. #endregion
  207. #region SetIsRdpEnabled
  208. public static void SetIsRdpEnabled(bool enabled) {
  209. if (enabled) {
  210. SetRdpRegistryValue(0);
  211. }
  212. else {
  213. SetRdpRegistryValue(1);
  214. }
  215. }
  216. #region private SetRdpRegistryValue
  217. private static void SetRdpRegistryValue(int value) {
  218. try {
  219. using (RegistryKey localMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default),
  220. rdpKey = localMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Terminal Server", true)) {
  221. if (!int.TryParse(rdpKey.GetValue("fDenyTSConnections").ToString(), out int currentValue)) {
  222. currentValue = -1;
  223. }
  224. //Value was not found do not proceed with change.
  225. if (currentValue == -1) {
  226. return;
  227. }
  228. else if (value == 1 && currentValue == 1) {
  229. NTMinerConsole.DevDebug("RDP is already disabled. No changes will be made.");
  230. return;
  231. }
  232. else if (value == 0 && currentValue == 0) {
  233. NTMinerConsole.DevDebug("RDP is already enabled. No changes will be made.");
  234. return;
  235. }
  236. else {
  237. rdpKey.SetValue("fDenyTSConnections", value);
  238. }
  239. }
  240. }
  241. catch {
  242. }
  243. }
  244. #endregion
  245. #endregion
  246. }
  247. }