以管理员身份运行流程并重定向流程输出 [英] run process as admin and redirect the process output

查看:85
本文介绍了以管理员身份运行流程并重定向流程输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我想以管理员身份运行流程,所以我使用runas作为流程信息的动词,我应该设置UseShellExecute = true,否则它将不会作为管理员打开。



除此之外,我想重定向过程输出,因此需要UseShellExecute = false!



你知道如何实现这个目标吗?

  var  outputBuilder =  new  StringBuilder(); 
var processStartInfo = new ProcessStartInfo();
processStartInfo.CreateNoWindow = true ;
processStartInfo.RedirectStandardOutput = true ;
processStartInfo.RedirectStandardError = true ;
processStartInfo.RedirectStandardInput = true ;
processStartInfo.UseShellExecute = false ;
processStartInfo.Arguments = / b + @ C:\ AutoInstaller \ myApp.bld;
processStartInfo.FileName = @ C:\Program Files(x86)\ ViisBuildPro8 \VisBuildCmd。 EXE;

processStartInfo.Verb = runas;

var process = new Process();

process.StartInfo = processStartInfo;

process.EnableRaisingEvents = true ;


process.OutputDataReceived + = 委托 object sender, DataReceivedEventArgs e)
{
outputBuilder.Append(e.Data);
outputBuilder.Append(Environment.NewLine);
};

process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
process.CancelOutputRead();





谢谢!

解决方案

< blockquote>看一下设置ProcessStartInfo域,LoadUserProfile,UserName和Password属性。



我刚试了一个例子,重定向工作正常。 .NET 3.5及更高版本具有System.Windows.Control.PasswordBox,这是获取密码的SecureString的简单方法。



在我考虑的时候,我最好用硬编码的管理员密码删除我的测试!



Alan。


Hello,

I want to run a process as an admin, so i used runas as the process info's verb and i should set UseShellExecute = true, else it won't be opened as an admin.

Beside that, i want to redirect the process output, hence the need for UseShellExecute = false!

do you have any idea how this could be accomplished?

var outputBuilder = new StringBuilder();
var processStartInfo = new ProcessStartInfo();
processStartInfo.CreateNoWindow = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.UseShellExecute = false;
processStartInfo.Arguments = "/b " + @"C:\AutoInstaller\myApp.bld";
processStartInfo.FileName = @"C:\Program Files (x86)\VisBuildPro8\VisBuildCmd.exe";

processStartInfo.Verb = "runas";

var process = new Process();

process.StartInfo = processStartInfo;

process.EnableRaisingEvents = true;


process.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e)
{
 outputBuilder.Append(e.Data);
 outputBuilder.Append(Environment.NewLine);
};

process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
process.CancelOutputRead();



Thanks!

解决方案

Take a look at setting the ProcessStartInfo Domain, LoadUserProfile, UserName and Password properties.

I've just tried out an example and redirection works correctly. .NET 3.5 and higher have the System.Windows.Control.PasswordBox which would be a simple way to get the SecureString for the password.

I'd better go and delete my test with the hard coded admin password while I'm thinking about it!

Alan.


这篇关于以管理员身份运行流程并重定向流程输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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