C#设置程序开机启动

本文介绍了如何使用C#获取当前用户权限,判断是否为管理员,以及在不同权限下编辑注册表实现应用程序的开机自启动。特别强调了正确设置启动路径和注意事项,包括可能存在的不同架构下的注册表路径和格式要求。
摘要由CSDN通过智能技术生成

1:获取当前用户:

 System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
            System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);

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

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

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:对注册表进行编辑,设置启动路径

 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:注销开机自启动功能(可选):

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

5:特别注意事项:

 1,虽然使用:

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

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

  • 7
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在 C# 程序中将应用程序设为开机启动,你可以使用 `Microsoft.Win32` 命名空间中的 `Registry` 类来修改注册表。下面是一个示例代码: ```csharp using Microsoft.Win32; using System; class Program { static void Main() { string appName = "MyApp"; // 应用程序的名称 string appPath = "C:\\Path\\To\\Your\\Application.exe"; // 应用程序的路径 // 创建或打开注册表项 RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); try { // 设置开机启动项 rk.SetValue(appName, appPath); Console.WriteLine("已将应用程序设置开机启动。"); } catch (Exception ex) { Console.WriteLine("设置开机启动时出错:" + ex.Message); } // 关闭注册表项 rk.Close(); Console.ReadLine(); } } ``` 上述代码中,我们使用 `Registry.CurrentUser.OpenSubKey()` 方法打开 `HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run` 注册表项,该项存储了当前用户的开机启动项。然后,我们使用 `SetValue()` 方法将应用程序的名称和路径设置为注册表项的值。 请注意,你需要将 `appName` 替换为你的应用程序的名称,`appPath` 替换为你的应用程序的完整路径。 运行上述代码后,应用程序将被添加到开机启动项中。在每次用户登录时,应用程序都会自动启动。 如果你想要从开机启动项中移除应用程序,可以使用 `RegistryKey.DeleteValue()` 方法。例如: ```csharp rk.DeleteValue(appName); ``` 以上代码将从注册表中删除指定的开机启动项。 请注意,修改注册表需要管理员权限。确保以管理员身份运行你的应用程序,或者在运行时获得管理员权限。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值