Esempio n. 1
0
        /// <summary>
        /// 开机自动启动
        /// 赋值快捷方式到[启动]文件夹
        /// </summary>
        /// <returns>开启或停用是否成功</returns>
        public static void SetSelfStart()
        {
            if (IsSetSelfStart())
            {
                return;
            }

            if (AppElvatedHelper.IsElvated())
            {
                try
                {
                    var exePath      = Process.GetCurrentProcess().MainModule.FileName;
                    var shortcutPath = GetShortCutPath();
                    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        = "";      // 启动参数
                    shortcut.IconLocation     = exePath;
                    shortcut.WorkingDirectory = System.IO.Path.GetDirectoryName(exePath);
                    shortcut.Description      = "";
                    shortcut.Save();
                    // 取消其他自启动
                    var di  = new FileInfo(shortcutPath).Directory;
                    var fis = di.GetFiles(SystemConfig.AppName + "_*");
                    if (fis?.Length > 0)
                    {
                        foreach (var fi in fis)
                        {
                            if (fi.FullName != shortcutPath)
                            {
                                File.Delete(fi.FullName);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                // 以高操作权限执行
                AppElvatedHelper.RunElvatedTask(StartupMode.SetSelfStart);
            }
        }
        private void BtnDisReg_OnClick(object sender, RoutedEventArgs e)
        {
            if (AppElvatedHelper.IsElvated())
            {
                SetSelfStartingHelper.UnsetSelfStart();
            }
            else
            {
                AppElvatedHelper.RunElvatedTask(StartupMode.UnsetSelfStart);
            }
            var task = new Task(() =>
            {
                Thread.Sleep(500);
                CheckBackGround();
            });

            task.Start();
        }
Esempio n. 3
0
        public static void UnsetSelfStart()
        {
            var shortcutPath = GetShortCutPath();

            if (File.Exists(shortcutPath))
            {
                if (AppElvatedHelper.IsElvated())
                {
                    try
                    {
                        File.Delete(shortcutPath);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    // 以高操作权限执行
                    AppElvatedHelper.RunElvatedTask(StartupMode.UnsetSelfStart);
                }
            }
        }