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

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

相关推荐
虾球xz4 分钟前
CppCon 2015 学习:3D Face Tracking and Reconstruction using Modern C++
开发语言·c++·学习·3d
林鸿群6 分钟前
C#子线程更新主线程UI及委托回调使用示例
开发语言·c#
o0向阳而生0o10 分钟前
63、.NET 异常处理
c#·.net·异常处理
SteveDraw3 小时前
C++动态链接库封装,供C#/C++ 等编程语言使用——C++动态链接库概述(总)
开发语言·c++·c#·封装·动态链接库
十五年专注C++开发3 小时前
设计模式之单例模式(二): 心得体会
开发语言·c++·单例模式·设计模式
flyair_China3 小时前
【云架构】
开发语言·php
Kookoos3 小时前
性能剖析:在 ABP 框架中集成 MiniProfiler 实现性能可视化诊断
后端·c#·.net·abp vnext·miniprofiler
Chef_Chen3 小时前
从0开始学习R语言--Day20-ARIMA与格兰杰因果检验
开发语言·学习·r语言
zh_xuan3 小时前
c++ std::pair
开发语言·c++