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";
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();
process.StartInfo = startInfo;
process.Start();
C/C++里, 可用ShellExecute或ShellExecuteEx, 把lpOperation/lpVerb设成"RunAs"就可
或脚本:
if (WScript.Arguments.Length >= 1)
{
app = WScript.Arguments(0);
args = "";
for (index = 1; index < WScript.Arguments.Length; index ++)
{
if (index > 1)
args += " "; //space
args += WScript.Arguments(index);
}
objShell = new ActiveXObject("Shell.Application");
if (objShell)
objShell.ShellExecute(app, args, "", "runas");
}
else
{
WScript.Echo("Usage: runas.js app, args");
}
{
app = WScript.Arguments(0);
args = "";
for (index = 1; index < WScript.Arguments.Length; index ++)
{
if (index > 1)
args += " "; //space
args += WScript.Arguments(index);
}
objShell = new ActiveXObject("Shell.Application");
if (objShell)
objShell.ShellExecute(app, args, "", "runas");
}
else
{
WScript.Echo("Usage: runas.js app, args");
}
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>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
也可以直接给程序添加一个如上的manifest文件,使程序以管理员权限运行。
本文转自jazka 51CTO博客,原文链接:http://blog.51cto.com/jazka/218312,如需转载请自行联系原作者