C# WinForm判断Win7下程序是否以管理员身份运行
来源:互联网 发布:sql update 编辑:程序博客网 时间:2024/05/16 00:46
Vista 和 Windows 7 操作系统为了加强安全,增加了 UAC(用户账户控制) 的机制,如果 UAC 被打开,用户即使是以管理员权限登录,其应用程序默认情况下也无法对系统目录,系统注册表等可能影响系统运行的设置进行写操作。这个机制大大增强了系统的安全性,但对应用程序开发者来说,我们不能强迫用户去关闭UAC,但有时我们开发的应用程序又需要以 Administrator 的方式运行,即 Win7 中 以 as administrator 方式运行,那么我们怎么来实现这样的功能呢?
我们在 win7 下运行一些安装程序时,会发现首先弹出一个对话框,让用户确认是否同意允许这个程序改变你的计算机配置,但我们编写的应用程序默认是不会弹出这个提示的,也无法以管理员权限运行。本文介绍了 C# 程序如何设置来提示用户以管理员权限运行。
首先在项目中增加一个 Application Manifest File
默认的配置如下:
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"
xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<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" />
If you want to utilize File and Registry Virtualization for backward
compatibility then delete the requestedExecutionLevel node.
-->
<requestedExecutionLevellevel="asInvoker" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</asmv1:assembly>
我们可以看到这个配置中有一个 requestedExecutionLevel 项,这个项用于配置当前应用请求的执行权限级别。这个项有3个值可供选择,如下表所示:
ValueDescriptionCommentasInvokerThe application runs with the same access token as the parent process.Recommended for standard user applications. Do refractoring with internal elevation points, as per the guidance provided earlier in this document.highestAvailableThe application runs with the highest privileges the current user can obtain.Recommended for mixed-mode applications. Plan to refractor the application in a future release.requireAdministratorThe application runs only for administrators and requires that the application be launched with the full access token of an administrator.Recommended 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)
- C# WinForm判断Win7下程序是否以管理员身份运行
- C# WinForm判断Win7下程序是否以管理员身份运行
- C# WinForm判断程序是否以管理员身份运行
- C# WinForm判断Win7下是否是管理员身份运行
- C# WinForm判断程序是否以管理员身份运行,UAC权限的提权与降权
- Winform程序在win7中默认以管理员身份运行
- 在Windows7上用C#判断程序是否以管理员身份运行
- [Winform]默认以管理员身份运行程序
- win7下让程序默认以管理员身份运行
- 判断进程是否"以管理员身份运行"
- 检查程序是否以管理员身份运行
- C#默认以管理员身份运行程序
- c# 程序以管理员身份运行
- C#默认以管理员身份运行程序
- C#默认以管理员身份运行程序
- c#程序以管理员身份运行
- C#默认以管理员身份运行程序
- C#如何以管理员身份运行程序
- Cocos2d-x学习笔记(16)(常见22种特效)
- Windows SOCKET编程 2-----(面向连接的通信TCP)---客户端
- 输入法之核心词典构建
- android 网络编程:显示从网络获取的图片
- OpenGL ES着色器语言之变量和数据类型和着色器流程
- C# WinForm判断Win7下程序是否以管理员身份运行
- acm第一套 (2)
- unity3d避免代码被反编译
- AWR解析报告
- 林志颖发福露肚腩 网友:看你不再消瘦很满足林志颖发福露肚腩 网友:看你不再消瘦很满足
- Linux 读写锁
- 第十六周自由练习项目——acm 复数类--重载运算符2+
- Memcached全面剖析–2.理解memcached的内存存储
- Dialog样式的Activity