14

ユーザーが管理者以外のプログラム内から管理者としてコマンド ライン ユーティリティを実行できるようにし、プログラムが出力を取得できるようにしたいと考えています。このユーティリティはサードパーティですが、私のプログラムと共に配布されています。

プログラムの出力をリダイレクトしたり、プログラムを管理者として実行したりできますが、両方を同時に実行することはできません。

現時点で作業を開始できる唯一のことは、cmd.exe を使用して出力をファイルにリダイレクトすることです。

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 + "\" > \"" + 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);

これには 2 つの問題があります。1) 一時ファイルを使用し、2) UAC は utility.exe ではなく cmd.exe 用です。これを行うためのより良い方法がきっとあるはずですか?

4

2 に答える 2