运行过程由非管理员应用程序管理员 [英] Run process as administrator from a non-admin application

查看:392
本文介绍了运行过程由非管理员应用程序管理员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个没有被以管理员身份运行的应用程序,我有以下代码:

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";

当我打电话的Process.Start(PROC),我没有得到一个弹出询问您是否允许以管理员身份运行,而该exe是不以管理员身份运行。

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.

我尝试添加一个app.manifest在myExePath找到可执行文件,并更新了requestedExecutionLevel到

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

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

通过更新app.manifest,上的Process.Start(PROC)电话,我得到一个异常,请求的操作需要提升。

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

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

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

我测试的Windows Server 2008 R2标准上。

I am testing on Windows Server 2008 R2 Standard.

推荐答案

必须使用的ShellExecute 。 ShellExecute的是一个知道如何启动 Consent.exe 为了提升仅API。

You must use ShellExecute. ShellExecute is the only API that knows how to launch Consent.exe in order to elevate.

在C#中,你叫的ShellExecute 的方法是使用过程。开始 UseShellExecute = TRUE 以及

In C#, the way you call ShellExecute is to use Process.Start along with 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:

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;
   }
}



红利观看




  • UAC - 什么。怎么样。为什么。。 UAC的架构,并解释说,的CreateProcess 不能做抬高,只能创建一个过程。 的ShellExecute 用谁知道如何启动Consent.exe,并Consent.exe是谁检查组策略选项之一的人。

  • Bonus Watching

    • UAC - What. How. Why.. The architecture of UAC, explaining that CreateProcess cannot do elevation, only create a process. ShellExecute is the one who knows how to launch Consent.exe, and Consent.exe is the one who checks group policy options.
    • 注意:释放到公共领域的任何代码。没有归属的需要。

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

      这篇关于运行过程由非管理员应用程序管理员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆