我想以提升的权限启动子进程(确切地说,是控制台应用程序),但隐藏窗口。
我执行以下步骤:
var info = new ProcessStartInfo(Assembly.GetEntryAssembly().Location)
{
UseShellExecute = true, // !
Verb = "runas",
};
var process = new Process
{
StartInfo = info
};
process.Start();
而且这个有效:
var identity = new WindowsPrincipal(WindowsIdentity.GetCurrent());
identity.IsInRole(WindowsBuiltInRole.Administrator); // returns true
但是,UseShellExecute = true
会创建一个新的窗口,并且我也无法重定向输出。
所以当我执行以下操作时:
var info = new ProcessStartInfo(Assembly.GetEntryAssembly().Location)
{
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false, // !
Verb = "runas"
};
var process = new Process
{
EnableRaisingEvents = true,
StartInfo = info
};
DataReceivedEventHandler actionWrite = (sender, e) =>
{
Console.WriteLine(e.Data);
};
process.ErrorDataReceived += actionWrite;
process.OutputDataReceived += actionWrite;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
这不会提升权限,而且上面的代码返回 false。为什么??
DataReceivedEventHandler actionWrite = ...
和process.ErrorDataReceived += actionWrite
。 - SLaks