技术思绪摘录旅行笔记
一般我们需要让某些使用非常频繁的软件,一开机,就运行起来,避免再去手动启动,除了用户自己添加启动项外,我们一般程序也会提供设置项,让用户更方便的来开启和关闭开机启动功能。

原理:

    把我们程序的快捷方式,创建一个到系统启动文件夹内。

准备:

    1、给我们项目添加引用:Com 中搜索 Windows Script Host Object Model

    2、创建操作辅助类:StartHelper.cs



开源是程序员的基本美德


    /// <summary>
    /// 开机启动 辅助类
    /// </summary>
    public class StartHelper
    {
        /// <summary>
        /// 快捷方式的文件名称
        /// </summary>
        public static string AppName = "ReleaseTool";
        /// <summary>
        /// 当前程序路径
        /// </summary>
        public static string AppFile = Application.ExecutablePath;
        /// <summary>
        /// 当前用户开机启动文件夹
        /// </summary>
        public static string Startup = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
        /// <summary>
        /// 开机启动
        /// </summary>
        /// <param name="directory">启动文件夹位置</param>
        /// <param name="shortcutName">程序名称</param>
        /// <param name="targetPath">指定目标路径</param>
        /// <param name="description">快捷方式备注</param>
        /// <param name="iconLocation">图标</param>
        /// <returns></returns>
        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, $"{shortcutName}.lnk");
                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 (Exception ex)
            {
                LogHelper.WriteLog(ex, "开机启动Create异常");
                return false;
            }
        }

        /// <summary>
        /// 删除开机启动项
        /// </summary>
        /// <param name="directory">启动文件夹位置</param>
        /// <param name="shortcutName">程序名称</param>
        /// <returns></returns>
        public static bool Delete(string directory, string shortcutName)
        {
            try
            {
                string shortcutPath = Path.Combine(directory, $"{shortcutName}.lnk");
                if (File.Exists(shortcutPath))
                {
                    File.Delete(shortcutPath);
                }
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex, "开机启动Delete异常");
                return false;
            }
        }

        /// <summary>
        ///是否开机启动
        /// </summary>
        /// <param name="directory">The directory.</param>
        /// <param name="shortcutName">Name of the shortcut.</param>
        /// <returns>
        ///   <c>true</c> if the specified directory is exist; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsExist(string directory, string shortcutName)
        {
            string shortcutPath = Path.Combine(directory, $"{shortcutName}.lnk");
            if (File.Exists(shortcutPath))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }


    使用方式:


1、判断是否已经开启了开机启动
bool iSstartUp = StartHelper.IsExist(StartHelper.Startup, StartHelper.AppName);//true false
2、开启开机启动
bool ret=StartHelper.Create(StartHelper.Startup, StartHelper.AppName, StartHelper.AppFile);//true false
3、关闭开机启动
bool ret=StartHelper.Delete(StartHelper.Startup, StartHelper.AppName);//true false


    注意:

        方法入参本可以由外部传入,此处为了方便,直接内置了,按需改动。 


CarsonIT 微信扫码关注公众号 策略、创意、技术

留下您的脚步

 

最近评论

查看更多>>

站点统计

总文章数:275 总分类数:18 总评论数:88 总浏览数:128.24万

精选推荐

阅读排行

友情打赏

请打开您的微信,扫一扫