/// <summary> /// Restarts the current process with highest privileges. /// </summary> /// <param name="cmdLineArgs"> /// The command-line arguments to use when starting the application. Use null /// to use the current arguments, which are already in use. /// </param> public static void RestartAsAdministrator(string cmdLineArgs = null) { if (IsAdministrator) { return; } var args = string.Empty; if (cmdLineArgs != null) { args = cmdLineArgs; } else { if (Log.DebugMode > 0) { args = $"/{Log.DebugKey} {Log.DebugMode} "; } args += EnvironmentEx.CommandLine(false); } using (ProcessEx.Start(PathEx.LocalPath, PathEx.LocalDir, args, true)) { Environment.ExitCode = 0; Environment.Exit(Environment.ExitCode); } }
/// <summary> /// Restarts the current process with non-elevated privileges. /// </summary> /// <param name="cmdLineArgs"> /// The command-line arguments to use when starting the application. Use null /// to use the current arguments, which are already in use. /// </param> public static void RestartAsNonAdministrator(string cmdLineArgs = null) { if (!IsAdministrator) { return; } var startArgs = string.Empty; if (cmdLineArgs != null) { startArgs = cmdLineArgs; } else { if (Log.DebugMode > 0) { startArgs = $"/{Log.DebugKey} {Log.DebugMode} "; } startArgs += EnvironmentEx.CommandLine(false); } var psi = new ProcessStartInfo { Arguments = startArgs, FileName = PathEx.LocalPath, Verb = "RunNotAs" }; using (ProcessEx.Start(psi)) { Environment.ExitCode = 0; Environment.Exit(Environment.ExitCode); } }