个性化阅读
专注于IT技术分析

如何在任何环境下强制基于C#的WinForms应用程序以管理员权限运行?

本文概述

由于各种原因, Windows中的UAC管理非常麻烦。最简单和通常的做法是, 某些应用程序将需要执行系统级任务, 例如停止或启动Windows服务甚至是你创建的服务。如果用户使用管理员权限运行该应用程序, 则一切将正常运行, 但是我们知道用户的情况!我们知道他们忘记了(我们也这样做)以必要的特权运行应用程序, 因此你不能总是期望得到同样的结果。相反, 你可能需要搜索一个安全的解决方案, 以保证你所提到的权限可以执行你的应用程序。

在本文中, 我们将简要介绍如何在基于C#的WinForms应用程序执行开始时请求管理员权限。

1.验证你的应用程序是否具有应用程序清单

第一步, 你需要验证你的应用程序是否确实在根目录中包含清单:

解决方案资源管理器C#WinForms

如果不存在, 则必须按照以下说明创建它。

如果没有, 请使用默认信息创建它

如果你没有在应用程序中注册提到的清单, 请继续创建它。在Visual Studio中打开你的项目, 然后右键单击你的项目, 单击”添加和新建项目”:

申请清单添加项目

在新对话框中, 从Visual C#项目中选择应用程序清单文件:

将应用程序清单添加到WinForms

这将在应用程序的根目录(即app.manifest)中创建一个内容相似的文件:

<?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="asInvoker" uiAccess="false" />
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
        <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />

            Specifying requestedExecutionLevel element will disable file and registry virtualization. 
            Remove this element if your application requires this virtualization for backwards
            compatibility.
        -->
        <requestedExecutionLevel level="asInvoker" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>

  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
      <!-- A list of the Windows versions that this application has been tested on
           and is designed to work with. Uncomment the appropriate elements
           and Windows will automatically select the most compatible environment. -->

      <!-- Windows Vista -->
      <!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->

      <!-- Windows 7 -->
      <!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->

      <!-- Windows 8 -->
      <!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->

      <!-- Windows 8.1 -->
      <!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->

      <!-- Windows 10 -->
      <!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->

    </application>
  </compatibility>

  <!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher
       DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need 
       to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should 
       also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config. -->
  <!--
  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
    </windowsSettings>
  </application>
  -->

  <!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
  <!--
  <dependency>
    <dependentAssembly>
      <assemblyIdentity
          type="win32"
          name="Microsoft.Windows.Common-Controls"
          version="6.0.0.0"
          processorArchitecture="*"
          publicKeyToken="6595b64144ccf1df"
          language="*"
        />
    </dependentAssembly>
  </dependency>
  -->

</assembly>

一旦知道你的应用程序中已包含此文件, 就可以继续执行最后一步。

2.请求管理员权限

为了以管理员权限启动应用程序, 你将需要更改trustInfo元素内的requestedExecutionLevel子节点的值。 requestedExecutionLevel节点标识应用程序请求执行的安全级别。该元素没有子元素, 具有以下2个属性:

级别要求。指示应用程序正在请求的安全级别。可能的值为:

  • asInvoker, 不要求其他权限。此级别不需要其他信任提示。

  • maximumAvailable, 请求父进程可用的最高权限。

  • requireAdministrator, 请求完全的管理员权限。

  • ClickOnce应用程序将仅使用asInvoker值安装。以任何其他值安装将失败。

uiAccess

可选的。指示应用程序是否需要访问受保护的用户界面元素。值为true或false, 默认值为false。仅已签名的应用程序的值应为true。

在默认清单中, 你将找到下一个值:

<requestedExecutionLevel  level="asInvoker" uiAccess="false" />

但是, 你需要将其更改为下一个, 以要求管理员权限:

<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />

将更改保存在app.manifest文件上并使用Visual Studio调试器启动应用程序后, 将看到以下警告(仅当你未以管理员身份运行Visual Studio时)。

Visual Studio C#WinForms管理员权限应用程序

编码愉快!

赞(0)
未经允许不得转载:srcmini » 如何在任何环境下强制基于C#的WinForms应用程序以管理员权限运行?

评论 抢沙发

评论前必须登录!