2

我们需要在正常进程中启动具有所需管理员权限(在其 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();
}
4

1 回答 1

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!>>")
    ...
于 2014-09-09T11:23:39.737 回答