重定向输入/输出时,Cmd不升高

时间:2014-07-16 21:39:06

标签: c# wpf cmd

我试图通过CMD命令运行自动安装。程序输出进度,我需要捕获该输出并在一个漂亮的窗口中计算总进度。根据我的理解,不可能同时提升和重定向。我试过了......

运行cmd并提升命令。

        var proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "cmd.exe",
                Verb = "runas",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardInput = true,
                RedirectStandardError = true,
                CreateNoWindow = true
            }
        };
        proc.Start();
        proc.StandardInput.WriteLine("command");

使用命令作为参数运行cmd

            var proc = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "cmd.exe",
                    Arguments = "/C " + "command",
                    Verb = "runas",
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    RedirectStandardInput = true,
                    RedirectStandardError = true,
                    CreateNoWindow = true
                }
            };
            proc.Start();

无效。这有什么窍门吗?捕获输入/输出后升高?我需要这个工作。 psexec可以实现吗?

1 个答案:

答案 0 :(得分:0)

如果您拥有安装程序,则可以重写它们,以便它们不使用标准流作为通信方式,例如named pipes。如果不这样做,您可以编写一个运行提升的包装程序(使用UseShellExecute运行它),不使用标准流,但运行安装程序并重定向其输入(在没有{{的情况下运行它们) 1}})通过命名管道或其他方式向非高速主程序报告进度。我希望这个图表能让它更清晰:

UseShellExecute
相关问题