• 将程序加到启动组


    将程序加到启动组
     
    有时需要将程序设置成随系统启动,最简单的一种就是放在 程序-->启动组里面。

    其实就是创建一个快捷方式而已, 呵呵

    我们需要一个 com 组件, 名叫 "Window Script Host Object Model ", 将它加入引用中

    代码就很简单了, 为了方便调用, 将它写成一个函数
    /**//// <summary>
            
    /// 将程序的快捷方式添加到启动组
            
    /// </summary>
            
    /// <param name="fullPath">程序全路径</param>
            private void AddShortCutToStartup(string fullPath)
            
    {
                
    if (string.IsNullOrEmpty(fullPath))
                    
    return;
                 IWshRuntimeLibrary.WshShell shell
    = new IWshRuntimeLibrary.WshShell();
                
    try
                
    {
                    
    //取得快捷方式的路径
                    
    //快捷方式其实是一个后缀为 lnk 的文件
                    string link = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup)
                 , Path.GetFileNameWithoutExtension(fullPath)
    + ".lnk");
                    
    if (!File.Exists(link))
                    
    {
                         IWshRuntimeLibrary.IWshShortcut shortCut
    = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(link);
                        
    //设置目标路径
                        
    //shortCut 还有很多方法,比如 HotKey, IconLocation 等等,不再赘述
                         shortCut.TargetPath = fullPath;
                         shortCut.WindowStyle
    = 1;
                         shortCut.Save();
                     }

                 }

                
    catch (Exception ex)
                
    {
                     log.Error(
    "AddShortCutToStartup Error: " + ex.Message);
                 }

             }

    如果要将本程序做成自启动, 只需要在 Form_Load 中加一行代码就行了

    AddShortCutToStartup(Application.ExecutablePath);
    作者:wpf之家
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    centos7系统初始化脚本
    git上传项目到github
    requests的使用
    zip函数
    mongodb基本操作
    mongodb的安装与配置启动(转)
    jupyter插件与主题
    map函数
    centos7 安装 ffmpeg
    centos7 下 yum 安装Nginx
  • 原文地址:https://www.cnblogs.com/wpf123/p/2347418.html
Copyright © 2020-2023  润新知