Vista 下以管理员权限运行程序 | 雨律在线

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.内容为

xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">

xmlns="urn:schemas-microsoft-com:asm.v3">


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



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

另附微软官方UAC编程文档:(Windows Vista Application Development Requirements for User Account Control CompatibilityWindows Vista Application Development Requirements for User Account Control Compatibility)
http://www.microsoft.com/downloads/details.aspx?FamilyID=ba73b169-a648-49af-bc5e-a2eebb74c16b&DisplayLang=en

 
目前有0条回应
Comment
Trackback
你目前的身份是游客,请输入昵称和电邮!