zoukankan      html  css  js  c++  java
  • 将程序加到启动组

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

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

    我们需要一个 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下搭建SVN
    Ubuntu设置telnet 远程登录(root权限)
    E: 无法打开锁文件 /var/lib/dpkg/lock-frontend
    使用ICMP搭建隧道(PingTunnel)
    Centos7安装Redis
    idea 激活方法
    Chrome 浏览器安装 ChroPath 插件
    jmeter引入外部jar包的方法
    maven安装
    eclipse集成 json editor plugin插件
  • 原文地址:https://www.cnblogs.com/wpf123/p/2347418.html
Copyright © 2011-2022 走看看