软件自启动方式c#

软件自启动的方式

参考文档:

 

建议方式:1.快捷方式,2.2CurrentUser注册表(测试代码见7、8章节)

1.  将软件的快捷方式放到开始文件夹里

当前用户:

C:\Users\lizj2\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

所有用户:

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup

优点:需要权限最小(生成快捷方式即可)

缺点:对于所有用户文件夹来说,需要管理员身份才能运行程序

2.  将软件放到注册表里

优点:很多程序都是这么做的,

缺点:需要管理员权限才能设置注册表

2.1  LocalMachine:对所有用户都适用

注册表里32位程序,local_machine::

HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Run

测试结果:32位机器无此项

注册表里64位程序,local_machine:

计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

2.2 current_user:对当前用户适用

注册表里64位\32位程序,current_user:

计算机\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

2.3 加载值程序(Load value Program):对所有用户适用

HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Run

2.4 RunOnce and RunOnceEx

HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnce

HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnceEx

HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce

HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnceEx

测试结果:不起作用

2.5 .WinLogOn

HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\Userinit”

测试结果:可以启动

“HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\Shell

测试结果:无效,电脑启动不了

2.6 策略key

HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\Run HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\Run.

测试结果:无效

2.7 BootExecute:用来启动时候检查系统

HKLM\System\CurrentControlSet\Control\Session Manager

测试结果:无效

3.  计划任务

测试结果:可以

4.  Win.ini:早期window版本适用(16位系统) win7以后已废弃

5.  组策略:计算机配置和用户配置里设置

组策略脚本:

6.  安装一个windows服务

7.  测试代码(注册表):

/// <summary>

        /// 自启动,注册表

        /// </summary>

        private static void AutoStartUseRegistryKey()

        {

            string appName = Process.GetCurrentProcess().MainModule.ModuleName;

            string appPath = Process.GetCurrentProcess().MainModule.FileName;

 

            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);

            if (key == null)

            {

                key = Registry.CurrentUser.CreateSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run");

            }

            key.SetValue("appName", appPath, RegistryValueKind.String);

            key.Close();

        }

8.  测试代码(快捷方式)

/// <summary>

        /// 自启动,启动目录,创建快捷方式

        /// </summary>

        static void AutoStartUseStartupFolder()

        {

            var startupFolder= Environment.GetFolderPath(Environment.SpecialFolder.Startup);

 

            var appName = Process.GetCurrentProcess().ProcessName;

 

            string shortcutPath = Path.Combine(startupFolder, $"{appName}.lnk");          //合成路径

            WshShell shell = new IWshRuntimeLibrary.WshShell();

            IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath);    //创建快捷方式对象

            shortcut.TargetPath = Process.GetCurrentProcess().MainModule.FileName;                                                               //指定目标路径

            shortcut.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;                                  //设置起始位置

            shortcut.WindowStyle = 1;                                                                          //设置运行方式,默认为常规窗口

            shortcut.Description = "";                                                             //设置备注

            //shortcut.IconLocation = string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation;    //设置图标路径

            shortcut.Save();                                                                                //保存快捷方式

        }

原文地址:https://www.cnblogs.com/congqiandehoulai/p/15566189.html