美文网首页
程序自启动

程序自启动

作者: 雪之梦_8f14 | 来源:发表于2019-07-11 11:00 被阅读0次

相关链接

三种方法

第一种:开始菜单启动

  • 原理: 将应用程序的可执行文件的快捷方式复制到系统或者用户的菜单启动目录下即可
  • 前提条件:需要引入com组件 Windows Script Host Object Model
  • 核心代码
 public static bool Create(string directory, string shortcutName, string targetPath,
                                  string description = null, string iconLocation = null)
        {
            try
            {
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                //添加引用 Com 中搜索 Windows Script Host Object Model
                string shortcutPath = Path.Combine(directory, string.Format("{0}.lnk", shortcutName));
                IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
                IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath);//创建快捷方式对象
                shortcut.TargetPath = targetPath;//指定目标路径
                shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath);//设置起始位置
                shortcut.WindowStyle = 1;//设置运行方式,默认为常规窗口
                shortcut.Description = description;//设置备注
                shortcut.IconLocation = string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation;//设置图标路径
                shortcut.Save();//保存快捷方式

                return true;
            }
            catch
            { }
            return false;
        }
  • 使用代码

        /// <summary>
        /// 通过创建快捷方式并放到菜单启动根目录下实现 开机自启动
        /// </summary>
        private void AutoRunFun1()
        {
            // 通过创建快捷方式并放到菜单启动根目录下实现 开机自启动
            var startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);

            var currentDirectory = Directory.GetCurrentDirectory();

            var str = Path.Combine(currentDirectory, "AutoRunApplication.exe");

            try
            {
                Create(startupPath, "AutoRunApplication", str);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }

private void CancelAutoRunFun1()
        {
            var startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);

            startupPath = Path.Combine(startupPath, "AutoRunApplication.lnk");

            if (File.Exists(startupPath))
            {
                File.Delete(startupPath);
            }
        }

第二种:注册表开机启动项

  • 代码最简单
// 添加到 当前登陆用户的 注册表启动项
RegistryKey RKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
RKey.SetValue("AppName", """" +@"C:\AppName.exe" + """");

 RegistryKey rKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
            rKey.DeleteValue("autorunapp");

第三种:Windows 计划任务方式启动

  • 略 见参考链接

相关文章

  • win10自启动管理

    虽然开机自启动程序会影响开机速度,不过将一些必用软件设置生开机自启动也是很爽的。 添加开机自启动 WIN键 + R...

  • 环境变量故障案列

    1.tomcat程序 开机自启动 2.STAR 原则后的故障: S:让tomcat 开机自启动,写入/etc/rc...

  • windows 自启动注册表项

    Run,RunEx和RunOnce 参考链接:全面揪出Windows系统中的自启动程序开机自启动的注册表键值有那些?

  • 让应用程序开机自启动

    定制类项目通常客户都会需要开机自启动程序的这个功能,那么要实现开机自启动应用程序,一般会有如下几种方式实现: sh...

  • 程序自启动

    class ProgramMonitor:"""判断程序是否已经挂掉"""def init(self):self....

  • 程序自启动

    相关链接 源码 参考网址 三种方法 第一种:开始菜单启动 原理: 将应用程序的可执行文件的快捷方式复制到系统或者用...

  • 树莓派3b+ 开机自启动+预约关机

    树莓派开机自启动 树莓派开机自启动python程序的方法有很多,这里介绍实验成功的方法——修改rc.local文件...

  • linux自启动设置

    开机自启动 linux开机自启动的程序在/etc/init.d/这个文件夹,里面的文件全部都是脚本文体. rc (...

  • linux自启动服务的几种方式

    RedHat4 自启动方式 一 通过服务的方式自启动 1.在/etc/init.d 下建立相关程序的启动脚本 ln...

  • Ubuntu 18.04添加开机自启

    Ubuntu 18.04添加开机自启地两种方式 一、通过命令行添加自启动程序 添加服务:添加这个服务并且开机自启动...

网友评论

      本文标题:程序自启动

      本文链接:https://www.haomeiwen.com/subject/qufrkctx.html