知识大全 C# WinForm判断程序是否以管理员身份运行

Posted

篇首语:青春不以山海为远,志愿只向家国未来。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 C# WinForm判断程序是否以管理员身份运行相关的知识,希望对你有一定的参考价值。

C# WinForm判断程序是否以管理员身份运行  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  Vista 和 Windows 操作系统为了加强安全 增加了 UAC(用户账户控制) 的机制 如果 UAC 被打开 用户即使是以管理员权限登录 其应用程序默认情况下也无法对系统目录 系统注册表等可能影响系统运行的设置进行写操作 这个机制大大增强了系统的安全性 但对应用程序开发者来说 我们不能强迫用户去关闭UAC 但有时我们开发的应用程序又需要以 Administrator 的方式运行 即 Win 中 以 as administrator 方式运行 那么我们怎么来实现这样的功能呢?

  我们在 win 下运行一些安装程序时 会发现首先弹出一个对话框 让用户确认是否同意允许这个程序改变你的计算机配置 但我们编写的应用程序默认是不会弹出这个提示的 也无法以管理员权限运行 本文介绍了 C# 程序如何设置来提示用户以管理员权限运行 首先在项目中增加一个 Application Manifest File       默认的配置如下   <?xml version=" " encoding="utf "?> <asmv :assembly manifestVersion=" " xmlns="urn:schemas microsoft :asm v "  xmlns:asmv ="urn:schemas microsoft :asm v " xmlns:asmv ="urn:schemas microsoft :asm v "  xmlns:xsi=" <assemblyIdentity version=" " name="MyApplication app"/> <trustInfo xmlns="urn:schemas microsoft :asm v "> <security> <requestedPrivileges xmlns="urn:schemas microsoft :asm v "> <!  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" /> If you want to utilize File and Registry Virtualization for backward patibility then delete the requestedExecutionLevel node > <requestedExecutionLevel level="asInvoker" uiAccess="false" /> </requestedPrivileges> </security> </trustInfo> </asmv :assembly>

  我们可以看到这个配置中有一个 requestedExecutionLevel 项 这个项用于配置当前应用请求的执行权限级别 这个项有 个值可供选择 如下表所示

   Value Description Comment asInvoker The application runs with the same access token as the parent process Remended for standard user applications Do refractoring with internal elevation points as per the guidance provided earlier in this document highestAvailable The application runs with the highest privileges the current user can obtain Remended for mixed mode applications Plan to refractor the application in a future release requireAdministrator The application runs only for administrators and requires that the application be launched with the full access token of an administrator Remended for administrator only applications Internal elevation points are not needed The application is already running elevated

  asInvoker : 如果选这个 应用程序就是以当前的权限运行

  highestAvailable: 这个是以当前用户可以获得的最高权限运行

  requireAdministrator: 这个是仅以系统管理员权限运行

  默认情况下是 asInvoker

  highestAvailable 和 requireAdministrator 这两个选项都可以提示用户获取系统管理员权限 那么这两个选项的区别在哪里呢?

  他们的区别在于 如果我们不是以管理员帐号登录 那么如果应用程序设置为 requireAdministrator 那么应用程序就直接运行失败 无法启动 而如果设置为 highestAvailable 则应用程序可以运行成功 但是是以当前帐号的权限运行而不是系统管理员权限运行 如果我们希望程序在非管理员帐号登录时也可以运行(这种情况下应该某些功能受限制) 那么建议采用 highestAvailable 来配置

  关于requestedExecutionLevel 设置的权威文档请参考下面链接

  Create and Embed an Application Manifest (UAC)

下面是修改后的配置文件   <?xml version=" " encoding="utf "?> <asmv :assembly manifestVersion=" " xmlns="urn:schemas microsoft :asm v "  xmlns:asmv ="urn:schemas microsoft :asm v " xmlns:asmv ="urn:schemas microsoft :asm v " xmlns:xsi=" <assemblyIdentity version=" " name="MyApplication app"/> <trustInfo xmlns="urn:schemas microsoft :asm v "> <security> <requestedPrivileges xmlns="urn:schemas microsoft :asm v "> <!  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" /> If you want to utilize File and Registry Virtualization for backward  patibility then delete the requestedExecutionLevel node > <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> </requestedPrivileges> </security> </trustInfo> </asmv :assembly>  

  配置文件修改后 我们运行应用程序 就会首先弹出这样一个提示框 点 Yes 后 程序才可以继续运行 并且获得系统管理员的权限

      下面再来看看程序如何知道当前运行在系统管理员权限还是非系统管理员权限 using System Security Principal   public static bool IsAdministrator() WindowsIdentity identity = WindowsIdentity GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); return principal IsInRole(WindowsBuiltInRole Administrator);   cha138/Article/program/net/201311/14263

相关参考

知识大全 在C#中跨线程访问Winform控件

在C#中跨线程访问Winform控件  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  在程序需要读

知识大全 简单的图片飘C#(Winform)

简单的图片飘C#(Winform)  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  //个Pict

知识大全 C#在winform中查找控件

C#在winform中查找控件  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  ///<su

按照演练过程中扮演的角色和承担的任务不同,可将应急演练人员分为五类。其中,依据演练方案及演练计划的要求,以各种不同身份诱导演练人员按响应程序行动,并不断给出情况或消息,供参演的指挥人员进行判断、提出对

按照演练过程中扮演的角色和承担的任务不同,可将应急演练人员分为五类。其中,依据演练方案及演练计划的要求,以各种不同身份诱导演练人员按响应程序行动,并不断给出情况或消息,供参演的指挥人员进行判断、提出对

按照演练过程中扮演的角色和承担的任务不同,可将应急演练人员分为五类。其中,依据演练方案及演练计划的要求,以各种不同身份诱导演练人员按响应程序行动,并不断给出情况或消息,供参演的指挥人员进行判断、提出对

按照演练过程中扮演的角色和承担的任务不同,可将应急演练人员分为五类。其中,依据演练方案及演练计划的要求,以各种不同身份诱导演练人员按响应程序行动,并不断给出情况或消息,供参演的指挥人员进行判断、提出对

知识大全 C# WinForm 读写XML文件

C#WinForm读写XML文件  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  建立一个WinF

知识大全 C# WinForm下DataGridView单元按钮列

C#WinForm下DataGridView单元按钮列  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧

知识大全 c#中使用多线程访问winform中控件

  我们在做winform应用的时候大部分情况下都会碰到使用多线程控制界面上控件信息的问题然而我们并不能用传统方法来做这个问题下面我将详细的介绍  首先来看传统方法  publicpartialcla

知识大全 C#中让程序只运行一个实例的操作方法

C#中让程序只运行一个实例的操作方法  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  让程序只运行

知识大全 C#窗体的应用

C#窗体的应用  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  在做Winform窗体程序的时候遇