C#设置程序开机启动

1:获取当前用户:

cs 复制代码
 System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
            System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);

2:判断当前用户是否是管理员如果是则直接启动否则通过Process启动:

(如果不这样处理直接使用非admin权限对注册表进行编辑操作程序将报异常)

cs 复制代码
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
            {
                //如果是管理员则直接启动
                Application.Run(new Form1());
            }
            else
            {
                System.Diagnostics.ProcessStartInfo startinfo = new System.Diagnostics.ProcessStartInfo();
                //启动的应用程序
                startinfo.FileName = Application.ExecutablePath;
                //设置启动动作,以管理员身份启动
                startinfo.Verb = "runas";
              var process=  System.Diagnostics.Process.Start(startinfo);
                Application.Exit();
            }

3:对注册表进行编辑,设置启动路径

cs 复制代码
 RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run",true);
                //当前启动的项目

                //string app = Application.ExecutablePath;
                //获取的路径格式为:D:\Program Files (x86)/360/360Safe/safemon/360tray.exe
                //该格式无法达到开机启动的目的。

                string app = System.Reflection.Assembly.GetExecutingAssembly().Location;
                //格式:D:\Program Files (x86)\360\360Safe\safemon\360tray.exe
                //该格式实现开机启动
      
                Registry.LocalMachine.CreateSubKey(@"SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN").SetValue("MyAngel", app, RegistryValueKind.String); 		//打开注册表中的现有项并设置其中的键值类型

4:注销开机自启动功能(可选):

cs 复制代码
 //删除该启动项
            RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", RegistryKeyPermissionCheck.ReadWriteSubTree);
            
            runKey.DeleteValue("MyAngel");
            runKey.Close();

5:特别注意事项:

1,虽然使用:

cs 复制代码
Registry.LocalMachine.CreateSubKey(@"SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN").SetValue("MyAngel", app, RegistryValueKind.String);

理论上添加的键值信息应该是存储在:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

实际上有可能存储在:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run

2,设置的值其格式必须注意:

SetValue("MyAngel", app, RegistryValueKind.String);

app存储的字符串格式只能是D:\Program Files (x86)\360\360Safe\safemon\360tray.exe

而不能是:D:\Program Files (x86)/360/360Safe/safemon/360tray.exe

如果格式异常则无法达到开机自启动的目的。

相关推荐
Liudef0619 小时前
基于Java的LLM长上下文数据预处理方案:实现128k上下文智能数据选择
java·开发语言·人工智能
sky-stars19 小时前
.NET 泛型编程(泛型类、泛型方法、泛型接口、泛型委托、泛型约束)
c#·.net·.netcore
小妖同学学AI19 小时前
Rust 深度解析:基本类型的“精确”艺术
开发语言·后端·rust
我命由我1234519 小时前
Guava - Guava 基本工具 Preconditions、Optional
java·服务器·开发语言·后端·java-ee·guava·后端框架
Python私教19 小时前
Rust 快速入门:从零到上手的系统指南
开发语言·后端·rust
ouliten20 小时前
C++笔记:std::variant
开发语言·c++·笔记
Joker1008520 小时前
仓颉 String 内存表示:从 UTF-8 小对象到零拷贝子串的完整旅程
开发语言
REDcker20 小时前
C++项目 OpenSSL 依赖最佳实践
开发语言·c++
一念&21 小时前
每日一个C语言知识:C 错误处理
c语言·开发语言·算法
国服第二切图仔21 小时前
Rust开发之使用panic!处理不可恢复错误
开发语言·后端·rust