我想启动一个具有提升权限但具有隐藏窗口的子进程(实际上是相同的控制台应用程序).
我做下一个:
var info = new ProcessStartInfo(Assembly.GetEntryAssembly().Location)
{
UseShellExecute = true, // !
Verb = "runas",
};
var process = new Process
{
StartInfo = info
};
process.Start();
Run Code Online (Sandbox Code Playgroud)
这工作:
var identity = new WindowsPrincipal(WindowsIdentity.GetCurrent());
identity.IsInRole(WindowsBuiltInRole.Administrator); // returns true
Run Code Online (Sandbox Code Playgroud)
但是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 …Run Code Online (Sandbox Code Playgroud) 我需要在我的c#应用程序中运行一个提升的命令并获得输出.
我正在创建一个新System.Diagnostics.Process实例,再加上一个新System.Diagnostics.ProcessStartInfo实例.
该命令被发送到cmd.exe,不幸的是,可能需要提升的用户访问(含义UseShellExecute = true和Verb = "runas"必须存在).
由于使用UseShellExecute = true,RedirectStandardOutput将无法正常工作.
重要的是我记录命令的输出,但我能看到的唯一方法是添加类似于> output.txt参数列表,然后调用System.IO.File.ReadAllText以读取结果.
任何人都可以想到更少的hacky方式?