Skip to content

Instantly share code, notes, and snippets.

@malcomvetter
Created July 20, 2018 03:44
Show Gist options
  • Select an option

  • Save malcomvetter/6ebf15e3c293e47267a178351a667c06 to your computer and use it in GitHub Desktop.

Select an option

Save malcomvetter/6ebf15e3c293e47267a178351a667c06 to your computer and use it in GitHub Desktop.
Programmatically call UAC elevation prompt
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);
}
}
}
@RokeJulianLockhart
Copy link

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 console project:

PS C:\Users\rokej\testproj> tree /f "$PWD"
Folder PATH listing for volume { Name: Windows, Identifier: SY0
Volume serial number is 2A4F-E07C
C:\USERS\ROKEJ\TESTPROJ
│   Program.cs
│   testproj.csproj
│   testproj.sln
│   
├───bin
│   └───Debug
│       └───net10.0
│               testproj.deps.json
│               testproj.dll
│               testproj.exe
│               testproj.pdb
│               testproj.runtimeconfig.json
│               
└───obj
    │   project.assets.json
    │   project.nuget.cache
    │   testproj.csproj.nuget.dgspec.json
    │   testproj.csproj.nuget.g.props
    │   testproj.csproj.nuget.g.targets
    │   
    └───Debug
        └───net10.0
            │   .NETCoreApp,Version=v10.0.AssemblyAttributes.cs
            │   apphost.exe
            │   testproj.AssemblyInfo.cs
            │   testproj.AssemblyInfoInputs.cache
            │   testproj.assets.cache
            │   testproj.csproj.CoreCompileInputs.cache
            │   testproj.csproj.FileListAbsolute.txt
            │   testproj.dll
            │   testproj.GeneratedMSBuildEditorConfig.editorconfig
            │   testproj.genruntimeconfig.cache
            │   testproj.GlobalUsings.g.cs
            │   testproj.pdb
            │   
            ├───ref
            │       testproj.dll
            │       
            └───refint
                    testproj.dll

Environment

  1. The OS (Windows)

    1. #!/usr/bin/env pwsh
      If ($IsWindows) { 
      	Get-ComputerInfo | Select-Object -Property @(
      		'OsName',
      		'OsOperatingSystemSKU',
      		'OsVersion',
      		'OsBuildNumber'
      	) | Format-List
      }
    2. OsName               : Microsoft Windows 11 Pro
      OsOperatingSystemSKU : 48
      OsVersion            : 10.0.26120
      OsBuildNumber        : 26120
  2. The DotNet SDK

    1. #!/usr/bin/env pwsh
      If ($IsWindows) { 
      	winget show Microsoft.DotNet.SDK.Preview
      }

      2

    2. Found Microsoft .NET SDK 10.0 Preview [Microsoft.DotNet.SDK.Preview]
      Version: 10.0.100-preview.5.25277.114
      Publisher: Microsoft Corporation
      Moniker: dotnet-sdk-preview
      Installer:
        Installer Type: burn
        Installer Url: https://builds.dotnet.microsoft.com/dotnet/Sdk/10.0.100-preview.5.25277.114/dotnet-sdk-10.0.100-preview.5.25277.114-win-x64.exe
        Installer SHA256: c6876933b5a994b8079dac06f22dc8bcf5179cd297ce1f0bf229b097343d5a40
        Offline Distribution Supported: true

Footnotes

  1. 6ebf15e3c293e47267a178351a667c06/1809ef03430c3be364ee555883564bf047ea9f25#file-program-cs-L1-L35

  2. github.com/microsoft/winget-cli/issues/3823

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment