Browse Source

增加设置开机启动项的方法

master
王厅 15 hours ago
parent
commit
93cd73c5e5
  1. 89
      Apewer.Windows/Internals/RegHelper.cs
  2. 13
      Apewer.Windows/WindowsUtility.cs

89
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
{
/// <summary>用户登录后的启动项。</summary>
const string RunKey = @"Software\Microsoft\Windows\CurrentVersion\Run";
const string Run = @"Software\Microsoft\Windows\CurrentVersion\Run";
/// <summary>HKEY_CURRENT_USER</summary>
/// <remarks>当前用户的信息。</remarks>
static RegistryKey CurrentUser { get => Registry.CurrentUser; }
public static RegistryKey CurrentUser { get => Registry.CurrentUser; }
/// <summary>HKEY_LOCAL_MACHINE</summary>
/// <remarks>系统信息,对所有用户生效,设置需要管理员权限。</remarks>
static RegistryKey LocalMachine { get => Registry.LocalMachine; }
static RegistryKey OpenSubKey(RegistryKey root, string key, bool write = false)
{
var segs = key.Split('\\', '/');
var queue = new Queue<string>(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; }
/// <summary>获取字符串。</summary>
/// <param name="root">注册表存储区。</param>
/// <param name="key">路径。</param>
/// <param name="name">名称。</param>
/// <returns>字符串的值。获取失败时返回 NULL 值。</returns>
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
/// <param name="name">名称。</param>
/// <param name="value">值。</param>
/// <returns>错误信息。设置成功时返回 NULL 值。</returns>
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
}
}
/// <summary>设置当前用户的启动项。</summary>
/// <remarks>错误消息。</remarks>
public static void SetRun(string name, string command)
{
var old = Get(CurrentUser, Run, name);
if (old.IsEmpty()) Set(CurrentUser, Run, name, command);
}
/// <summary>取消当前用户的启动项。</summary>
/// <remarks>错误消息。</remarks>
public static void CancelRun(string name)
{
Set(CurrentUser, Run, name, null);
}
/// <summary>已启用自动启动。</summary>
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);
}
}

13
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();
}
/// <summary>设置当前用户的启动项。</summary>
/// <param name="name">启动项的名称。</param>
/// <param name="command">执行的命令。</param>
public static void SetRun(string name, string command) => RegHelper.SetRun(name, command);
/// <summary>取消当前用户的启动项。</summary>
/// <param name="name">启动项的名称。</param>
public static void CancelRun(string name) => RegHelper.CancelRun(name);
#endregion
#region ARP

Loading…
Cancel
Save