diff --git a/Apewer.Windows/Internals/RegHelper.cs b/Apewer.Windows/Internals/RegHelper.cs
index d24d190..2303b67 100644
--- a/Apewer.Windows/Internals/RegHelper.cs
+++ b/Apewer.Windows/Internals/RegHelper.cs
@@ -1,10 +1,6 @@
-using Apewer;
-using Microsoft.Win32;
+using Microsoft.Win32;
using System;
-using System.Collections.Generic;
using System.IO;
-using System.Linq;
-using System.Text;
using System.Windows.Forms;
namespace Apewer.Internals
@@ -15,52 +11,30 @@ namespace Apewer.Internals
{
/// 用户登录后的启动项。
- const string RunKey = @"Software\Microsoft\Windows\CurrentVersion\Run";
+ const string Run = @"Software\Microsoft\Windows\CurrentVersion\Run";
/// HKEY_CURRENT_USER
/// 当前用户的信息。
- static RegistryKey CurrentUser { get => Registry.CurrentUser; }
+ public static RegistryKey CurrentUser { get => Registry.CurrentUser; }
/// HKEY_LOCAL_MACHINE
/// 系统信息,对所有用户生效,设置需要管理员权限。
- static RegistryKey LocalMachine { get => Registry.LocalMachine; }
-
- static RegistryKey OpenSubKey(RegistryKey root, string key, bool write = false)
- {
- var segs = key.Split('\\', '/');
- var queue = new Queue(segs);
- var rkey = root;
- var check = write ? RegistryKeyPermissionCheck.ReadWriteSubTree : RegistryKeyPermissionCheck.ReadSubTree;
- while (queue.Count > 0)
- {
- var name = queue.Dequeue();
- var sub = rkey.OpenSubKey(name, check);
- if (sub == null)
- {
- if (!write) return null;
- rkey = rkey.CreateSubKey(name);
- }
- else
- {
- rkey = sub;
- }
- }
- return rkey;
- }
+ public static RegistryKey LocalMachine { get => Registry.LocalMachine; }
/// 获取字符串。
/// 注册表存储区。
/// 路径。
/// 名称。
/// 字符串的值。获取失败时返回 NULL 值。
- static string Get(RegistryKey root, string key, string name)
+ public static string Get(RegistryKey root, string key, string name)
{
try
{
- var rkey = OpenSubKey(root, key, false);
- if (rkey == null) return null;
-
- var names = rkey.GetValueNames().ToList();
+#if !NET20
+ using
+#endif
+ var rkey = root.OpenSubKey(key, RegistryKeyPermissionCheck.ReadSubTree);
+ var names = rkey.GetSubKeyNames();
if (names.Contains(name))
{
var obj = rkey.GetValue(name, null);
@@ -78,15 +52,16 @@ namespace Apewer.Internals
/// 名称。
/// 值。
/// 错误信息。设置成功时返回 NULL 值。
- static string Set(RegistryKey root, string key, string name, string value)
+ public static string Set(RegistryKey root, string key, string name, string value)
{
try
{
- var rkey = OpenSubKey(root, key, true);
- if (rkey == null) return "无法打开子键。";
-
- var apps = rkey.GetValueNames();
- if (string.IsNullOrEmpty(value)) rkey.DeleteValue(name, true);
+#if !NET20
+ using
+#endif
+ var rkey = root.OpenSubKey(key, RegistryKeyPermissionCheck.ReadWriteSubTree);
+ var apps = rkey.GetSubKeyNames();
+ if (value.IsEmpty()) rkey.DeleteValue(name, true);
else rkey.SetValue(name, value, RegistryValueKind.String);
return null;
}
@@ -96,20 +71,36 @@ namespace Apewer.Internals
}
}
+ /// 设置当前用户的启动项。
+ /// 错误消息。
+ public static void SetRun(string name, string command)
+ {
+ var old = Get(CurrentUser, Run, name);
+ if (old.IsEmpty()) Set(CurrentUser, Run, name, command);
+ }
+
+ /// 取消当前用户的启动项。
+ /// 错误消息。
+ public static void CancelRun(string name)
+ {
+ Set(CurrentUser, Run, name, null);
+ }
+
/// 已启用自动启动。
- public static bool AutoRun
+ public static bool UserAutoRun
{
get
{
- var exePath = Application.ExecutablePath;
- var exeName = Path.GetFileNameWithoutExtension(exePath);
- return Get(CurrentUser, RunKey, exeName) == exePath;
+ var path = Application.ExecutablePath;
+ var name = Path.GetFileNameWithoutExtension(path);
+ var value = Get(CurrentUser, Run, name);
+ return value == path;
}
set
{
- var exePath = Application.ExecutablePath;
- var exeName = Path.GetFileNameWithoutExtension(exePath);
- Set(CurrentUser, RunKey, exeName, value ? exePath : null);
+ var path = Application.ExecutablePath;
+ var name = Path.GetFileNameWithoutExtension(path);
+ Set(CurrentUser, Run, name, value ? path : null);
}
}
diff --git a/Apewer.Windows/WindowsUtility.cs b/Apewer.Windows/WindowsUtility.cs
index ea721a9..1dde123 100644
--- a/Apewer.Windows/WindowsUtility.cs
+++ b/Apewer.Windows/WindowsUtility.cs
@@ -2,6 +2,7 @@
using System.Management;
#endif
+using Apewer.Internals;
using Apewer.Internals.Interop;
using Apewer.Models;
using Microsoft.Win32;
@@ -9,17 +10,16 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
+using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
-using System.Threading;
using System.Windows.Forms;
using static Apewer.Internals.Interop.Constant;
using static Apewer.Internals.Interop.AdvApi32;
using static Apewer.Internals.Interop.Kernel32;
using static Apewer.Internals.Interop.User32;
-using System.Net;
namespace Apewer
{
@@ -988,6 +988,15 @@ namespace Apewer
return vuninstallpath.ToArray();
}
+ /// 设置当前用户的启动项。
+ /// 启动项的名称。
+ /// 执行的命令。
+ public static void SetRun(string name, string command) => RegHelper.SetRun(name, command);
+
+ /// 取消当前用户的启动项。
+ /// 启动项的名称。
+ public static void CancelRun(string name) => RegHelper.CancelRun(name);
+
#endregion
#region ARP