I'm writing a c# program that should ran a proccess and print the output to the console or file.
But the exe I want to run must run as admin.
I saw that to run an exe as admin i need to set useShellExecute to true. but to enable output redirection i need to set it to false.
What can i do to achieve both? Thank you!
In this code I get the error printed to the console (because UseShellExecute=false ), and when changed to true - the program stops.
ProcessStartInfo proc = new ProcessStartInfo();
proc.UseShellExecute = false;
proc.WorkingDirectory = Environment.CurrentDirectory;
proc.FileName = "aaa.exe";
proc.RedirectStandardError = true;
proc.RedirectStandardOutput = true;
proc.Verb = "runas";
Process p = new Process();
p.StartInfo = proc;
p.Start();
while (!p.StandardOutput.EndOfStream)
{
string line = p.StandardOutput.ReadLine();
Console.WriteLine("*************");
Console.WriteLine(line);
}
runas theAdminID "myCommand.exe > someFile.txt"
. Then just read the file. – BobRodes