Created
July 20, 2018 03:44
-
-
Save malcomvetter/6ebf15e3c293e47267a178351a667c06 to your computer and use it in GitHub Desktop.
Programmatically call UAC elevation prompt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Diagnostics; | |
| using System.Security.Principal; | |
| namespace RunAs | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| if (!IsAdministrator()) | |
| { | |
| // Restart as admin | |
| var exe = Process.GetCurrentProcess().MainModule.FileName; | |
| var startInfo = new ProcessStartInfo(exe) | |
| { | |
| Verb = "runas" | |
| }; | |
| try | |
| { | |
| Process.Start(startInfo); | |
| } catch { } | |
| return; | |
| } | |
| Console.WriteLine("Running as admin!"); | |
| Console.ReadKey(); | |
| } | |
| private static bool IsAdministrator() | |
| { | |
| var identity = WindowsIdentity.GetCurrent(); | |
| var principal = new WindowsPrincipal(identity); | |
| return principal.IsInRole(WindowsBuiltInRole.Administrator); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Effect And Evidence
@malcomvetter, it 1 doesn't for me:
Recording.2025-06-18.010911.mp4
Configuration
It's an otherwise nigh-default
dotnet new consoleproject:Environment
The OS (Windows)
The DotNet SDK
2
Footnotes
6ebf15e3c293e47267a178351a667c06/1809ef03430c3be364ee555883564bf047ea9f25#file-program-cs-L1-L35↩github.com/microsoft/winget-cli/issues/3823↩