gpt4 book ai didi

c# - 以更高的权限启动进程并读取其输出

转载 作者:太空宇宙 更新时间:2023-11-03 13:17:03 24 4
gpt4 key购买 nike

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

最佳答案

我最终使用了命名管道
服务器:

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!>>")
...

关于c# - 以更高的权限启动进程并读取其输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25740739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com