【发布时间】:2020-07-13 08:33:22
【问题描述】:
我的 WPF 应用程序中只有几个选项需要管理员权限。我想避免强制以管理员身份运行程序,所以有没有机会在运行时要求管理员权限,只有在需要此权限的操作的情况下?
【问题讨论】:
-
这真的很老了,但它回答了你的问题吗? stackoverflow.com/a/2282613/982149
标签: c# wpf runtime administrator rights
我的 WPF 应用程序中只有几个选项需要管理员权限。我想避免强制以管理员身份运行程序,所以有没有机会在运行时要求管理员权限,只有在需要此权限的操作的情况下?
【问题讨论】:
标签: c# wpf runtime administrator rights
我认为您无法提升现有流程。但是我在 powershell 5 中找到了一种在新的提升进程中启动我的脚本的方法。希望这会有所帮助
# 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;
}
【讨论】:
您可以将应用程序清单文件(app.manifest)添加到您的项目中
并将 requestedExecutionLevel 设置为 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>
【讨论】: