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

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

相关推荐
Ray Liang11 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Scout-leaf3 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530144 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
mudtools5 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的5 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21885 天前
.NET 本地Db数据库-技术方案选型
windows·c#
郑州光合科技余经理5 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
lindexi5 天前
dotnet DirectX 通过可等待交换链降低输入渲染延迟
c#·directx·d2d·direct2d·vortice
feifeigo1235 天前
matlab画图工具
开发语言·matlab
dustcell.5 天前
haproxy七层代理
java·开发语言·前端