我们需要在正常流程中启动具有所需管理员权限(在其app.manifest中定义)的流程并读取其输出。我看到它的方式,为了启动具有更高权限的进程,我们需要设置UseShellExecute = true
。但这阻止我们接收标准输出
这是我的代码片段,但它需要当前进程也具有管理员权限(我们负担不起,因为它是一个巨大的应用程序,这是唯一需要提升的部分):
if (bAdmin)
{
Process p = new Process
{
StartInfo =
{
Arguments = "-GetRestorePoints",
FileName = Environment.CurrentDirectory + @"\Hamekare.AdminSettings.exe",
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
RedirectStandardOutput = true,
UseShellExecute = false
}
};
p.OutputDataReceived += p_OutputDataReceived;
p.Start();
p.BeginOutputReadLine();
}
答案 0 :(得分:1)
我最终使用了Named Pipes
服务器:
PipeSecurity ps = new PipeSecurity();
PipeAccessRule psRule = new PipeAccessRule(@"Everyone", PipeAccessRights.ReadWrite, System.Security.AccessControl.AccessControlType.Allow);
ps.AddAccessRule(psRule);
var server = new NamedPipeServerStream("HKBackUpRestorePipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous, 1, 1, ps);
server.WaitForConnection();
StreamWriter writer = new StreamWriter(server);
writer.WriteLine ...
writer.Flush();
客户:
var client = new NamedPipeClientStream("HKBackUpRestorePipe");
client.Connect();
StreamReader reader = new StreamReader(client);
StreamWriter writer = new StreamWriter(client);
string line;
while ((line = reader.ReadLine()) != "\t\t<<Finished!>>")
...