private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            string launcherPath = Assembly.GetEntryAssembly().Location;

            EnableSkipUAC.IsChecked   = SkipUACHelper.IsSkipUACTaskExist(launcherPath);
            EnableAutoStart.IsChecked = AutoStartHelper.IsAutoStartEnabled(launcherPath);
        }
 private void EnableSkipUAC_Unchecked(object sender, RoutedEventArgs e)
 {
     try
     {
         SkipUACHelper.DeleteSkipUACTask();
     }
     catch (Exception ex)
     {
         MessageBox.Show("Delete skip uac task failed.\r\n" + ex, "NoUACLauncher", MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
        private void EnableSkipUAC_Checked(object sender, RoutedEventArgs e)
        {
            string launcherPath = Assembly.GetEntryAssembly().Location;

            if (SkipUACHelper.IsSkipUACTaskExist(launcherPath))
            {
                return;
            }

            try
            {
                SkipUACHelper.CreateSkipUACTask(launcherPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Create skip uac task failed.\r\n" + ex, "NoUACLauncher", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Esempio n. 4
0
        protected override void OnStartup(StartupEventArgs e)
        {
            string launchPath = Assembly.GetEntryAssembly().Location;

            if (!UACHelper.IsProcessElevated())
            {
                // Current process run without elevated
                // Try to run new process through taskschedule
                if (SkipUACHelper.IsSkipUACTaskExist(launchPath))
                {
                    try
                    {
                        SkipUACHelper.RunSkipUACTask(launchPath, RunSkipUACArgument);
                    }
                    catch (Exception ex)
                    {
                        SaveElevateProcessFailedMessage(ex);
                        MessageBox.Show("Run skip uac task failed.", "NoUACLauncher", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
                else
                {
                    ProcessStartInfo psi = new ProcessStartInfo();
                    psi.FileName  = Assembly.GetEntryAssembly().Location;
                    psi.Arguments = RunAdminArgument;
                    psi.Verb      = "runas"; // runas代表要向用户请求admin权限

                    try
                    {
                        Process.Start(psi);
                    }
                    catch (Exception ex)
                    {
                        SaveElevateProcessFailedMessage(ex);
                        if (!(ex is Win32Exception && (uint)(ex as Win32Exception).ErrorCode == ERROR_USER_CANCELLED))
                        {
                            MessageBox.Show("Start elevate process failed.", "NoUACLauncher", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }

                Shutdown();
                return;
            }
            else
            {
                // Process run with elevated
                if (e.Args.Contains(RunAdminArgument))
                {
                    StartMode = "Run admin"; // 通过向用户请求获得admin权限启动
                }
                else if (e.Args.Contains(RunSkipUACArgument))
                {
                    StartMode = "Run skip uac"; // 通过计划任务获得admin权限启动
                }
                else
                {
                    StartMode = "Run elevated"; // 程序被用户或其他程序以admin权限启动
                }
            }

            base.OnStartup(e);
        }