Only a few options in my WPF application, need administrator rights. I'd like to avoid compulsion to run the program as an administrator, so is there any opportunity to ask for administrator privileges in runtime, only in case of operation which requires this privileges?
Asked
Active
Viewed 1,716 times
3 Answers
2
I don't think you can elevate an existing process. But I found a way in powershell 5 to start my script in a new elevated process. Hope this helps
# Get the ID and security principal of the current user account
$myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent();
$myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID);
# Get the security principal for the administrator role
$adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator;
# Check to see if we are currently running as an administrator
if ($myWindowsPrincipal.IsInRole($adminRole))
{
# We are running as an administrator, so change the title and background colour to indicate this
$Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)";
$Host.UI.RawUI.BackgroundColor = "DarkBlue";
Clear-Host;
}
else
{
# We are not running as an administrator, so relaunch as administrator
# Create a new process object that starts PowerShell
$newProcess = New-Object System.Diagnostics.ProcessStartInfo "PowerShell";
# Specify the current script path and name as a parameter with added scope and support for scripts with spaces in it's path
$newProcess.Arguments = "& '" + $script:MyInvocation.MyCommand.Path + "'"
# Indicate that the process should be elevated
$newProcess.Verb = "runas";
# Start the new process
[System.Diagnostics.Process]::Start($newProcess);
# Exit from the current, unelevated, process
Exit;
}
1
You can add to your project Application Manifest File (app.manifest)
and set requestedExecutionLevel to highestAvailable"/>
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- UAC Manifest Options
If you want to change the Windows User Account Control level replace the
requestedExecutionLevel node with one of the following. -->
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
<!-- requestedExecutionLevel level="asInvoker" uiAccess="false" /-->
<!-- requestedExecutionLevel level="requireAdministrator" uiAccess="false" /-->
</requestedPrivileges>
</security>
</trustInfo>
</assembly>

Anton
- 718
- 6
- 11
-
Well, it's not what I want to reach. Today when I was installing VirtualBox, installer asked me for administrator rights while installation process, not at start and I'm curious how have they achived that, because I need something similar in my program. – Ether Apr 01 '20 at 15:39
-
At runtime, you would have to create a new process of your application, which is running with administrator privileges. – BionicCode May 21 '22 at 08:21
0
- Add a manifest file to the following location.
- Change Line 19 to the following code in manifest.
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
- It will prompt for administrator right upon starting up.

Yew Hong Tat
- 644
- 6
- 10
-
Well, it's not what I want because as I said, I'd like to avoid compulsion to run the program as an administrator. Only in case of operations which need admin rights I'd like to please user for admin rights. – Ether Apr 01 '20 at 15:34