简体   繁体   中英

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

I am running processes from C# using the following code;

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. 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.

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. 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. This was solved by just launching Unity directly and a bug report has been filed against Unity hub.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM