예제 #1
0
        private void UtilitiesOnRestartRequested(object sender, RestartEventArgs e)
        {
            List <string> argsList = new();

            argsList.AddRange(StartupArguments);
            if (e.ExtraArgs != null)
            {
                argsList.AddRange(e.ExtraArgs.Except(argsList));
            }
            string args    = argsList.Any() ? "-ArgumentList " + string.Join(',', argsList) : "";
            string command =
                $"-Command \"& {{Start-Sleep -Milliseconds {(int) e.Delay.TotalMilliseconds}; " +
                "(Get-Process 'Artemis.UI').kill(); " +
                $"Start-Process -FilePath '{Constants.ExecutablePath}' -WorkingDirectory '{Constants.ApplicationFolder}' {args}}}\"";

            // Elevated always runs with RunAs
            if (e.Elevate)
            {
                ProcessStartInfo info = new()
                {
                    Arguments      = command.Replace("}\"", " -Verb RunAs}\""),
                    WindowStyle    = ProcessWindowStyle.Hidden,
                    CreateNoWindow = true,
                    FileName       = "PowerShell.exe"
                };
                Process.Start(info);
            }
            // Non-elevated runs regularly if currently not elevated
            else if (!IsElevated)
            {
                ProcessStartInfo info = new()
                {
                    Arguments      = command,
                    WindowStyle    = ProcessWindowStyle.Hidden,
                    CreateNoWindow = true,
                    FileName       = "PowerShell.exe"
                };
                Process.Start(info);
            }
            // Non-elevated runs via a utility method is currently elevated (de-elevating is hacky)
            else
            {
                string powerShell = Path.Combine(Environment.SystemDirectory, "WindowsPowerShell", "v1.0", "powershell.exe");
                ProcessUtilities.RunAsDesktopUser(powerShell, command, true);
            }

            // Lets try a graceful shutdown, PowerShell will kill if needed
            Execute.OnUIThread(() => Application.Current.Shutdown());
        }