コード例 #1
0
        /// <summary>
        /// 开机自动启动
        /// 赋值快捷方式到[启动]文件夹
        /// </summary>
        /// <returns>开启或停用是否成功</returns>
        public static void SetSelfStart()
        {
            // 先确保有操作权限
            AppElvatedHelper.ElvateApp(StartupMode.SetSelfStart);
            try
            {
                var startUpPath  = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup);
                var shortcutPath = System.IO.Path.Combine(startUpPath, string.Format("{0}.lnk", "上班统计"));
                //string shortcutPath = Process.GetCurrentProcess().MainModule.FileName + ".lnk";//System.IO.Path.Combine(startUpPath, string.Format("{0}.lnk", "上班统计"));

                var exePath = Process.GetCurrentProcess().MainModule.FileName;
                if (File.Exists(shortcutPath))
                {
                    File.Delete(shortcutPath);
                }
                var shell    = new IWshRuntimeLibrary.WshShell();
                var shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath);
                shortcut.TargetPath       = exePath;                                   // exe路径
                shortcut.Arguments        = "1";                                       // 启动参数
                shortcut.IconLocation     = exePath;
                shortcut.WorkingDirectory = System.IO.Path.GetDirectoryName(exePath);; //软件所在文件夹
                shortcut.Description      = "上班统计";
                shortcut.Save();
                MessageBox.Show("已在启动文件夹内创建快捷方式。");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
コード例 #2
0
 public static void UnsetSelfStart()
 {
     // 先确保有操作权限
     AppElvatedHelper.ElvateApp(StartupMode.UnsetSelfStart);
     try
     {
         var startUpPath  = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup);
         var shortcutPath = System.IO.Path.Combine(startUpPath, string.Format("{0}.lnk", "上班统计"));
         if (File.Exists(shortcutPath))
         {
             File.Delete(shortcutPath);
         }
         MessageBox.Show("已删除自启动。");
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }