Vista UAC编程: 以管理员权限运行程序

1、在Vista里, 你可以用鼠标右击某个应用程序(例如cmd.exe), 再选择"Run As Administrator"(在旧版本里是"Run Elevated")来以管理员权限运行它.

2、在程序(或其快捷方式)的属性Compatibility中选择Run this program as an administrator来运行

3、代码中

下面的C#代码会以管理员权限运行c:\test\script.cmd, 当然你会看到一个UAC对话框, 让你确认.

             ProcessStartInfo startInfo = new ProcessStartInfo();
             startInfo.FileName = "cmd.exe";
             startInfo.Arguments = "/c c:\\test\\script.cmd";
             startInfo.UseShellExecute = true;
             startInfo.Verb = "RunAs";

             Process process = new Process();
             process.StartInfo = startInfo;
             process.Start();

C/C++里, 可用ShellExecute或ShellExecuteEx, 把lpOperation/lpVerb设成"RunAs"就可

4、在应用程序rc中加入RT_MANIFEST类型资源,ID号为1.内容为

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
       <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
      </requestedPrivileges>
    </security>
</trustInfo>
</assembly>

也可以直接给程序添加一个如上的manifest文件,使程序以管理员权限运行。

 

另附微软官方UAC编程文档:(Windows Vista Application Development Requirements for User Account Control CompatibilityWindows Vista Application Development Requirements for User Account Control Compatibility)




[本日志由 JiaJia 于 2008-11-27 10:10 AM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: UAC Vista 权限
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.