C# 从非管理员应用程序以管理员身份运行进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16926232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:08:23  来源:igfitidea点击:

Run process as administrator from a non-admin application

c#uacrunasprocessstartinfo

提问by jkh

From an application that is not being run as administrator, I have the following code:

从不是以管理员身份运行的应用程序中,我有以下代码:

ProcessStartInfo proc = new ProcessStartInfo();
proc.WindowStyle = ProcessWindowStyle.Normal;
proc.FileName = myExePath;
proc.CreateNoWindow = false;
proc.UseShellExecute = false;
proc.Verb = "runas";

When I call Process.Start(proc), I do not get a pop up asking for permission to run as administrator, and the exe is not run as administrator.

当我调用 Process.Start(proc) 时,我没有弹出要求以管理员身份运行的权限,并且 exe 没有以管理员身份运行。

I tried adding an app.manifest to the executable found at myExePath, and updated the requestedExecutionLevel to

我尝试将 app.manifest 添加到在 myExePath 中找到的可执行文件,并将 requestsExecutionLevel 更新为

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

With the updated app.manifest, on the Process.Start(proc) call, I get an exception, "The requested operation requires elevation."

使用更新的 app.manifest,在 Process.Start(proc) 调用中,我收到一个异常,“请求的操作需要提升。”

Why isn't the .Verb action not setting administrator privileges?

为什么 .Verb 操作没有设置管理员权限?

I am testing on Windows Server 2008 R2 Standard.

我正在 Windows Server 2008 R2 Standard 上进行测试。

采纳答案by Ian Boyd

You mustuse ShellExecute. ShellExecute is the only API that knows how to launch Consent.exein order to elevate.

必须使用ShellExecute. ShellExecute 是唯一知道如何启动Consent.exe以提升的API 。

Sample (.NET) Source Code

示例 (.NET) 源代码

In C#, the way you call ShellExecuteis to use Process.Startalong with UseShellExecute = true:

在 C# 中,您调用的方式ShellExecute是与Process.Start一起使用UseShellExecute = true

private void button1_Click(object sender, EventArgs e)
{
   ProcessStartInfo info = new ProcessStartInfo(@"C:\Windows\Notepad.exe");
   info.UseShellExecute = true;
   info.Verb = "runas";
   Process.Start(info);
}

If you want to be a good developer, you can catch when the user clicked No:

如果你想成为一名优秀的开发者,你可以在用户点击No时捕捉:

private void button1_Click(object sender, EventArgs e)
{
   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;
   }
}

Bonus Watching

奖金观看

  • UAC - What. How. Why.. The architecture of UAC, explaining that CreateProcesscannot do elevation, only create a process. ShellExecuteis the one who knows how to launch Consent.exe, and Consent.exe is the one who checks group policy options.
  • UAC - 什么。如何。为什么。. UAC的架构,说明CreateProcess不能做提升,只能创建一个流程。ShellExecute是知道如何启动 Consent.exe 的人,而 Consent.exe 是检查组策略选项的人。

Note: Any code released into public domain. No attribution required.

注意:任何发布到公共领域的代码。不需要归属。