font-family:'Microsoft YaHei'; font-size:24px">一、经由过程设置文件完成以管理员身份运转顺序
Vista 和 Windows 7 操纵体系为了增强平安,增添了 UAC(用户账户掌握) 的机制,假如 UAC 被翻开,用户纵然是以管理员权限登录,其运用顺序默许情况下也没法对体系目次,体系注册表等能够影响体系运转的设置举行写操纵。这个机制大大增强了体系的平安性,但对运用顺序开辟者来讲,我们不能强制用户去封闭UAC,但偶然我们开辟的运用顺序又需要以 Administrator 的体式格局运转,即 Win7 中 以 as administrator 体式格局运转,那末我们怎样来完成如许的功用呢?
我们在 win7 下运转一些装置顺序时,会发明起首弹出一个对话框,让用户确认是不是赞同许可这个顺序转变你的计算机设置,但我们编写的运用顺序默许是不会弹出这个提醒的,也没法以管理员权限运转。本文引见了 C# 顺序怎样设置来提醒用户以管理员权限运转。
起首在项目中增添一个 Application Manifest File
默许的设置以下:
<?xml version="1.0" encoding="utf-8"?> <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. --> <requestedExecutionLevel level="asInvoker" uiAccess="false" /> </requestedPrivileges> </security> </trustInfo> </asmv1:assembly>
我们能够看到这个设置中有一个 requestedExecutionLevel 项,这个项用于设置当前运用要求的实行权限级别。这个项有3个值可供挑选,以下表所示:
Value | Description | Comment |
asInvoker | The 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. |
highestAvailable | The 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. |
requireAdministrator | The 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)
下面是修改后的设置文件:
<?xml version="1.0" encoding="utf-8"?> <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. --> <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> </requestedPrivileges> </security> </trustInfo> </asmv1:assembly>
设置文件修改后,我们运转运用顺序,就会起首弹出如许一个提醒框,点 Yes 后,顺序才能够继承运转,而且取得体系管理员的权限。
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
下面再来看看顺序怎样晓得当前运转在体系管理员权限还黑白体系管理员权限:
public static bool IsAdministrator() { WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); return principal.IsInRole(WindowsBuiltInRole.Administrator); }
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
这段代码能够用于推断当前顺序是不是运转在体系管理员权限下。假如设置为 asInvoker,在win7 下,这个函数会返回 false ,假如是 requireAdministrator 则返回 true。
二、经由过程编程以管理员身份运转顺序
在读写注册表“HKEY_LOCAL_MACHINE\SOFTWARE\”下的项时,明显注册表中有,但顺序OpenSubKey一直返回Null,考虑到能够是因为权限的缘由,因而我以管理员身份运转了一次,效果测试胜利!本来真的是权限的题目,因而就在顺序内里加入了默许以管理员身份运转的代码。下面让我们看看是怎样完成的吧!
顺序默许以管理员身份运转
static void Main(string[] Args) { /** * 当前用户是管理员的时刻,直接启动运用顺序 * 假如不是管理员,则运用启动对象启动顺序,以确保运用管理员身份运转 */ //取得当前登录的Windows用户标示 System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent(); //建立Windows用户主题 Application.EnableVisualStyles(); System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity); //推断当前登录用户是不是为管理员 if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)) { //假如是管理员,则直接运转 Application.EnableVisualStyles(); Application.Run(new Form1()); } else { //建立启动对象 System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); //设置运转文件 startInfo.FileName = System.Windows.Forms.Application.ExecutablePath; //设置启动参数 startInfo.Arguments = String.Join(" ", Args); //设置启动行动,确保以管理员身份运转 startInfo.Verb = "runas"; //假如不是管理员,则启动UAC System.Diagnostics.Process.Start(startInfo); //退出 System.Windows.Forms.Application.Exit(); } }
翻开顺序集里的Program.cs文件,并将个中Main要领中的代码替换为以上代码即可完成顺序默许以管理员身份运转。
以上就是细致引见C#默许以管理员身份运转顺序示例代码的细致内容,更多请关注ki4网别的相干文章!