RegisterHelper.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. namespace HXX.Scanner.Common
  9. {
  10. public class RegisterHelper
  11. {
  12. /// <summary>
  13. /// 开机启动测试
  14. /// </summary>
  15. /// <param name="isAuto"></param>
  16. /// <param name="showinfo"></param>
  17. public static void AutoStart(bool isAuto = true, bool showinfo = true)
  18. {
  19. try
  20. {
  21. if (isAuto == true)
  22. {
  23. RegistryKey R_local = Registry.CurrentUser;//RegistryKey R_local = Registry.CurrentUser;
  24. RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  25. R_run.SetValue("scannerClient", Application.ExecutablePath);
  26. R_run.Close();
  27. R_local.Close();
  28. }
  29. else
  30. {
  31. RegistryKey R_local = Registry.CurrentUser;//RegistryKey R_local = Registry.CurrentUser;
  32. RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  33. R_run.DeleteValue("scannerClient", false);
  34. R_run.Close();
  35. R_local.Close();
  36. }
  37. }
  38. catch (Exception)
  39. {
  40. if (showinfo)
  41. MessageBox.Show("您需要管理员权限修改", "提示");
  42. }
  43. }
  44. public static bool GetRun()
  45. {
  46. //RegistryKey Key1;
  47. //Key1 = Registry.CurrentUser;
  48. //RegistryKey myreg1 = Key1.OpenSubKey("software\\AnencStatus");
  49. //string status = EncodeClass.Decode_base64(myreg1.GetValue("AnencStatus").ToString(), AnencKey);
  50. //if (status == "true")
  51. // return true;
  52. RegistryKey R_local = Registry.CurrentUser;//RegistryKey R_local = Registry.CurrentUser;
  53. RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  54. var re=R_run.GetValue("scannerClient");
  55. R_run.Close();
  56. R_local.Close();
  57. if (re == null)
  58. {
  59. return false;
  60. }
  61. return true;
  62. }
  63. public static RegistryKey CreateSubKey(RegDomain _domain,string name)
  64. {
  65. ///创建基于注册表基项的节点
  66. RegistryKey key = GetRegDomain(_domain);
  67. // 要创建的注册表项的节点
  68. RegistryKey sKey= IsSubKeyExist(_domain, name);
  69. if (sKey == null)
  70. {
  71. sKey = key.CreateSubKey(name);
  72. key.Close();
  73. return sKey;
  74. }
  75. else
  76. {
  77. key.Close();
  78. return sKey;
  79. }
  80. }
  81. public static bool WriteRegeditKey(RegistryKey key,string name,string content)
  82. {
  83. //RegistryKey software = key.CreateSubKey(key.Name);
  84. var software = key.OpenSubKey(key.Name, true);
  85. software.SetValue(name, content);
  86. key.Close();
  87. return true;
  88. }
  89. protected static RegistryKey IsSubKeyExist(RegDomain _domain, string name)
  90. {
  91. ///判断注册表项名称是否为空,如果为空,返回false
  92. if (name == string.Empty || name == null)
  93. {
  94. return null;
  95. }
  96. ///检索注册表子项
  97. ///如果sKey为null,说明没有该注册表项不存在,否则存在
  98. RegistryKey sKey = OpenSubKey(_domain,name);
  99. return sKey;
  100. }
  101. protected static RegistryKey OpenSubKey(RegDomain _domain, string name)
  102. {
  103. ///判断注册表项名称是否为空
  104. if (name == string.Empty || name == null)
  105. {
  106. return null;
  107. }
  108. ///创建基于注册表基项的节点
  109. RegistryKey key = GetRegDomain(_domain);
  110. ///要打开的注册表项的节点
  111. RegistryKey sKey = null;
  112. ///打开注册表项
  113. sKey = key.OpenSubKey(name);
  114. ///关闭对注册表项的更改
  115. key.Close();
  116. ///返回注册表节点
  117. return sKey;
  118. }
  119. protected static RegistryKey GetRegDomain(RegDomain regDomain)
  120. {
  121. ///创建基于注册表基项的节点
  122. RegistryKey key;
  123. #region 判断注册表基项域
  124. switch (regDomain)
  125. {
  126. case RegDomain.ClassesRoot:
  127. key = Registry.ClassesRoot; break;
  128. case RegDomain.CurrentUser:
  129. key = Registry.CurrentUser; break;
  130. case RegDomain.LocalMachine:
  131. key = Registry.LocalMachine; break;
  132. case RegDomain.User:
  133. key = Registry.Users; break;
  134. case RegDomain.CurrentConfig:
  135. key = Registry.CurrentConfig; break;
  136. case RegDomain.PerformanceData:
  137. key = Registry.PerformanceData; break;
  138. default:
  139. key = Registry.LocalMachine; break;
  140. }
  141. #endregion
  142. return key;
  143. }
  144. }
  145. }