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

118 lines
4.0 KiB

2 years ago
  1. using Apewer;
  2. using Microsoft.Win32;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace Apewer.Internals
  10. {
  11. /// <summary>注册表。</summary>
  12. static class RegHelper
  13. {
  14. /// <summary>用户登录后的启动项。</summary>
  15. const string RunKey = @"Software\Microsoft\Windows\CurrentVersion\Run";
  16. /// <summary>HKEY_CURRENT_USER</summary>
  17. /// <remarks>当前用户的信息。</remarks>
  18. static RegistryKey CurrentUser { get => Registry.CurrentUser; }
  19. /// <summary>HKEY_LOCAL_MACHINE</summary>
  20. /// <remarks>系统信息,对所有用户生效,设置需要管理员权限。</remarks>
  21. static RegistryKey LocalMachine { get => Registry.LocalMachine; }
  22. static RegistryKey OpenSubKey(RegistryKey root, string key, bool write = false)
  23. {
  24. var segs = key.Split('\\', '/');
  25. var queue = new Queue<string>(segs);
  26. var rkey = root;
  27. var check = write ? RegistryKeyPermissionCheck.ReadWriteSubTree : RegistryKeyPermissionCheck.ReadSubTree;
  28. while (queue.Count > 0)
  29. {
  30. var name = queue.Dequeue();
  31. var sub = rkey.OpenSubKey(name, check);
  32. if (sub == null)
  33. {
  34. if (!write) return null;
  35. rkey = rkey.CreateSubKey(name);
  36. }
  37. else
  38. {
  39. rkey = sub;
  40. }
  41. }
  42. return rkey;
  43. }
  44. /// <summary>获取字符串。</summary>
  45. /// <param name="root">注册表存储区。</param>
  46. /// <param name="key">路径。</param>
  47. /// <param name="name">名称。</param>
  48. /// <returns>字符串的值。获取失败时返回 NULL 值。</returns>
  49. static string Get(RegistryKey root, string key, string name)
  50. {
  51. try
  52. {
  53. var rkey = OpenSubKey(root, key, false);
  54. if (rkey == null) return null;
  55. var names = rkey.GetValueNames().ToList();
  56. if (names.Contains(name))
  57. {
  58. var obj = rkey.GetValue(name, null);
  59. var str = obj as string;
  60. return str;
  61. }
  62. }
  63. catch { }
  64. return null;
  65. }
  66. /// <summary>设置字符串,指定 value 为 NULL 可删除该值。</summary>
  67. /// <param name="root">注册表存储区。</param>
  68. /// <param name="key">路径。</param>
  69. /// <param name="name">名称。</param>
  70. /// <param name="value">值。</param>
  71. /// <returns>错误信息。设置成功时返回 NULL 值。</returns>
  72. static string Set(RegistryKey root, string key, string name, string value)
  73. {
  74. try
  75. {
  76. var rkey = OpenSubKey(root, key, true);
  77. if (rkey == null) return "无法打开子键。";
  78. var apps = rkey.GetValueNames();
  79. if (string.IsNullOrEmpty(value)) rkey.DeleteValue(name, true);
  80. else rkey.SetValue(name, value, RegistryValueKind.String);
  81. return null;
  82. }
  83. catch (Exception ex)
  84. {
  85. return ex.Message;
  86. }
  87. }
  88. /// <summary>已启用自动启动。</summary>
  89. public static bool AutoRun
  90. {
  91. get
  92. {
  93. var exePath = Application.ExecutablePath;
  94. var exeName = Path.GetFileNameWithoutExtension(exePath);
  95. return Get(CurrentUser, RunKey, exeName) == exePath;
  96. }
  97. set
  98. {
  99. var exePath = Application.ExecutablePath;
  100. var exeName = Path.GetFileNameWithoutExtension(exePath);
  101. Set(CurrentUser, RunKey, exeName, value ? exePath : null);
  102. }
  103. }
  104. }
  105. }