Run new process as admin and read standard output
<p>I want to allow users to run a command line utility as administrator from within my non-admin program and for my program to get the output. The utility is third-party but is distributed with my programme.</p>

<p>I can <a href="http://stackoverflow.com/questions/206323/how-to-execute-command-line-in-c-get-std-out-results">redirect the output of a program</a> and I can <a href="http://stackoverflow.com/questions/133379/elevating-process-privilege-programatically">run a program as administrator</a> but I can't do both at the same time.</p>

<p>The only thing that I can get to work at the moment is using cmd.exe to redirect the output to a file, e.g.:</p>

<pre><code>using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Reflection;

string appDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string utilityPath = Path.Combine(appDirectory, "tools", "utility.exe");
string tempFile = Path.GetTempFileName();

Process p = new Process();
// hide the command window
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.FileName = "cmd.exe";
// run the tool, redirect the output to the temp file and then close.
p.StartInfo.Arguments = " /C \"\"" + utilityPath + "\" &gt; \"" + tempFile + "\"\"";
p.StartInfo.Verb = "runas"; // run as administrator
p.Start();
p.WaitForExit();

// get the output, delete the file and show the output to the user
string output = File.ReadAllText(tempFile);
File.Delete(tempFile);
MessageBox.Show(output);
</code></pre>

<p>This has two problems: 1) it uses a temporary file and 2) the UAC is for cmd.exe rather then utility.exe. There must surely be a better way to do this?</p>
