c# 设置程序开机启动 AutoStartHelper
using Microsoft.Win32;
using System;
using System.Reflection;
namespace Boaway.Platform.DataCollection.Client.Helper
{
public static class AutoStartHelper
{
//程序的名称
//public static string SoftWareName = "Raink.KeyPressedView";
public static string SoftWareName = Assembly.GetExecutingAssembly().GetName().Name;
public static void SetStartWithStartUp()
{
string path = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
// 针对当前用户添加注册表启动项
RegistryKey rk = Registry.CurrentUser;
//注册表位置
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
// 检测是否之前有设置自启动了,如果设置了,就看值是否一样
string old_path = (string)rk2.GetValue(SoftWareName);
if (path != old_path)
{
//写入值-避免空格导致问题
rk2.SetValue(SoftWareName, $"\"{path}\"");
//MessageBox.Show("设置开机自启成功", "KeyPressedView");
}
//else
//{
// MessageBox.Show("已设置过开机自启", "KeyPressedView");
//}
rk2.Close();
rk.Close();
}
public static bool ExistStartWithStartUp()
{
string path = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
// 针对当前用户添加注册表启动项
RegistryKey rk = Registry.CurrentUser;
//注册表位置
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
// 检测是否之前有设置自启动了,如果设置了,就看值是否一样
string old_path = (string)rk2.GetValue(SoftWareName);
rk2.Close();
rk.Close();
if (path!=old_path)
{
return false;
}
else
{
return true;
}
}
public static void CancelStartWithStartUp()
{
//string path = AppDomain.CurrentDomain.BaseDirectory;
RegistryKey rk = Registry.CurrentUser;
//相同的注册表位置
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
//获取现有值
//string old_path = (string)rk2.GetValue(SoftWareName);
//删除值
rk2.DeleteValue(SoftWareName, false);
//MessageBox.Show("已取消开启自启", "KeyPressedView");
rk2.Close();
rk.Close();
}
}
}
留待后查,同时方便他人
联系我:renhanlinbsl@163.com
联系我:renhanlinbsl@163.com
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南