运行提升过程

时间:2011-12-31 21:05:58

标签: c# process admin-rights

我正在尝试使用以下代码运行cmd命令:

ProcessStartInfo cmd = new ProcessStartInfo("cmd.exe");
cmd.RedirectStandardInput = true;
cmd.RedirectStandardOutput = true;
cmd.RedirectStandardError = true;
cmd.UseShellExecute = false;
cmd.CreateNoWindow = true;
cmd.WindowStyle = ProcessWindowStyle.Hidden;
Process exec = Process.Start(cmd);
exec.StandardInput.WriteLine("sc create \"BaliService\" binPath= \"{0}\\BaliService.exe\"", Directory.GetCurrentDirectory());

此命令需要管理员privelages,如果我以管理员身份运行cmd并键入命令,它可以正常工作,但不是当我以管理员身份运行此应用程序时。我添加了

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

到每次打开exe时提示uac的清单文件。

我在这方面已经看到了多个问题,他们似乎都建议在提升的应用程序下运行的任何进程都具有相同的权限,但这对我不起作用。

我尝试了cmd.Verb = "runas";,但没有骰子。

2 个答案:

答案 0 :(得分:13)

您需要将UseShellExecute设置为true以使Verb得到尊重,并且必须将其设置为“false”才能重定向标准输出。你不能两者兼顾。

我非常确定Windows也不允许您在管理/非管理安全边界上重定向标准输入/输出/错误。您必须找到一种不同的方式来从以管理员身份运行的程序中获取输出。

我没有读过这篇文章,但这可能会为您提供更多信息:http://www.codeproject.com/KB/vista-security/UAC__The_Definitive_Guide.aspx

答案 1 :(得分:2)

您是否尝试将管理凭据分配给ProcessStartInfo对象?

SecureString password = new SecureString();
password.AppendChar('p');
password.AppendChar('w');
cmd.UserName = "admin";
cmd.Password = password;