从不以管理员身份运行的应用程序中,我有以下代码:
ProcessStartInfo proc = new ProcessStartInfo();
proc.WindowStyle = ProcessWindowStyle.Normal;
proc.FileName = myExePath;
proc.CreateNoWindow = false;
proc.UseShellExecute = false;
proc.Verb = "runas";
当我调用 Process.Start(proc) 时,我没有弹出请求以管理员身份运行的权限,并且 exe 不以管理员身份运行。
我尝试将 app.manifest 添加到 myExePath 中找到的可执行文件中,并将 requestsExecutionLevel 更新为
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
使用更新后的 app.manifest,在 Process.Start(proc) 调用中,我收到一个异常:“请求的操作需要提升”。
为什么 .Verb 操作没有设置管理员权限?
我正在 Windows Server 2008 R2 Standard 上进行测试。
You must use ShellExecute
。 ShellExecute 是唯一知道如何启动的 APIConsent.exe
为了提升。
示例 (.NET) 源代码
在 C# 中,调用方式ShellExecute
是使用Process.Start
随着UseShellExecute = true
:
private void button1_Click(object sender, EventArgs e)
{
//Public domain; no attribution required.
ProcessStartInfo info = new ProcessStartInfo(@"C:\Windows\Notepad.exe");
info.UseShellExecute = true;
info.Verb = "runas";
Process.Start(info);
}
如果你想成为一名优秀的开发者,你可以捕捉用户点击的时间No:
private void button1_Click(object sender, EventArgs e)
{
//Public domain; no attribution required.
const int ERROR_CANCELLED = 1223; //The operation was canceled by the user.
ProcessStartInfo info = new ProcessStartInfo(@"C:\Windows\Notepad.exe");
info.UseShellExecute = true;
info.Verb = "runas";
try
{
Process.Start(info);
}
catch (Win32Exception ex)
{
if (ex.NativeErrorCode == ERROR_CANCELLED)
MessageBox.Show("Why you no select Yes?");
else
throw;
}
}
奖励观看
-
UAC——什么。如何。为什么。。 UAC的架构,解释一下
CreateProcess
不能做提升,只能创建一个进程。ShellExecute
是知道如何启动 Consent.exe 的人,而 Consent.exe 是检查组策略选项的人。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)