简体   繁体   English

当UseShellExecute == false时,如何在C#进程上强制输出标准输出?

[英]How do I force standard output on a C# Process when UseShellExecute == false?

I am running processes from C# using the following code; 我正在使用以下代码从C#运行进程;

private static void ExecuteShellCMD(string workingDir, string commandWithArgs, bool bWait = true)
{
    ProcessStartInfo info = new ProcessStartInfo();
    info.Verb = "runas";
    info.FileName = "cmd.exe";
    info.WorkingDirectory = workingDir;
    info.Arguments = "/C " + commandWithArgs;
    info.UseShellExecute = false;
    using (Process myProcess = Process.Start(info))
    {
        if (bWait)
        {
            myProcess.WaitForExit();
        }

        int ExitCode = myProcess.ExitCode;

        //Log exit code here.
    }
}

It loads an elevated command window and executes the code/bat file I pass it, but without logging anything to the console. 它会加载提升的命令窗口并执行我传递的代码/ bat文件,但不会在控制台中记录任何内容。 This doesn't appear to be consistent on other machines, and has worked in the past on my machine, and I wondered if anyone had any ideas about how I can consistently make this Process just print logs into the command window the process makes. 在其他计算机上这似乎并不一致,并且过去在我的计算机上也可以使用,我想知道是否有人对我如何能够始终如一地执行此过程有任何想法,只是将日志打印到该过程创建的命令窗口中。

I can see logs if I set UseShellExecute = true but then can't use Verb without accepting the elevation prompt which is undesirable. 如果设置UseShellExecute = true我可以看到日志,但是如果不接受不希望出现的海拔提示,就不能使用Verb

I have tried looking for solutions around the web, and I am aware that I can redirect the output using other settings. 我尝试过在网上寻找解决方案,并且我知道可以使用其他设置来重定向输出。 Most of the questions and tutorials on this subject seem to deal with redirecting the ouput to somewhere else but I want to be able to keep track of the progress in the command window itself. 关于此主题的大多数问题和教程似乎都涉及将输出重定向到其他地方,但是我希望能够在命令窗口本身中跟踪进度。

Perhaps I have missed an command line argument or similar? 也许我错过了命令行参数或类似的东西?

Turns out this was actually a bug in Unity Hub. 原来这实际上是Unity Hub中的错误。 The process and output were working fine, however when ran from a Unity instance that was launched from Unity Hub it took control of the output and didn't release it. 流程和输出工作正常,但是当从Unity Hub启动的Unity实例运行时,它控制了输出,但没有释放它。 This was solved by just launching Unity directly and a bug report has been filed against Unity hub. 只需直接启动Unity即可解决此问题,并且已经向Unity中心提交了错误报告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将系统PATH与Process.UseShellExecute = false一起使用? - How do I use the system PATH with Process.UseShellExecute=false? Ctrl + C即使UseShellExecute:false也不应关闭进程 - Ctrl + C should not close process even when UseShellExecute:false 如何在没有'useshellexecute = false'的过程中进行读写 - How to write and read in a process without 'useshellexecute = false' UseShellExecute = false 时进程不会退出,但 UseShellExecute = true 时退出 - Process won't exit when UseShellExecute = false, but exits when UseShellExecute = true 当 useshellexecute = false 时 Process.start 找不到文件 - Process.start does not find file when useshellexecute = false 如何使用C#绑定标准输入和输出? - How do I tie into the standard input and output with C#? 当 UseShellExecute 为真时,如何读取控制台 output? - How to read console output when UseShellExecute is true? 在 C# / .NET 进程中重定向标准错误时缺少 Output - Missing Output when Redirecting Standard Error in C# / .NET Process 在C#中,我如何拥有一个覆盖标准process.WaitForExit()的process.WaitForExit(time)? - In C# how do I have a process.WaitForExit(time) that overrides a standard process.WaitForExit()? 我可以在C#中将流程的标准输出和标准错误与控制台窗口的输出合并吗? - Can I merge the standard output and standard error of a Process with the output of a console window in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM