Click here to Skip to main content
14,699,886 members
Please Sign up or sign in to vote.
3.33/5 (2 votes)
See more:
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!
Posted
Comments
Bernhard Hiller 14-May-14 2:55am
   
You use the "ASP.NET" tag. Where (server/client) do you want to run that code with what effect?

1 solution

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.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900