【问题标题】:WPF application - ask for administrator rights in runtimeWPF 应用程序 - 在运行时请求管理员权限
【发布时间】:2020-07-13 08:33:22
【问题描述】:

我的 WPF 应用程序中只有几个选项需要管理员权限。我想避免强制以管理员身份运行程序,所以有没有机会在运行时要求管理员权限,只有在需要此权限的操作的情况下?

【问题讨论】:

标签: c# wpf runtime administrator rights


【解决方案1】:

我认为您无法提升现有流程。但是我在 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;
}

【讨论】:

    【解决方案2】:

    您可以将应用程序清单文件(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>
    

    【讨论】:

    • 好吧,这不是我想要达到的。今天当我安装 VirtualBox 时,安装程​​序在安装过程中询问我管理员权限,而不是在开始时,我很好奇他们是如何实现这一点的,因为我的程序中需要类似的东西。
    【解决方案3】:
    1. 将清单文件添加到以下位置。

    1. 将第 19 行更改为清单中的以下代码。

    &lt;requestedExecutionLevel level="requireAdministrator" uiAccess="false" /&gt;

    1. 启动时会提示管理员权限。

    【讨论】:

    • 嗯,这不是我想要的,因为正如我所说,我想避免强迫以管理员身份运行程序。仅在需要管理员权限的操作的情况下,我想请用户获得管理员权限。
    猜你喜欢
    • 2011-09-19
    • 2021-07-27
    • 2021-07-17
    • 1970-01-01
    • 2018-05-01
    相关资源
    最近更新 更多