简体   繁体   中英

How can I run a command line app in C# and redirect/display its standard output while preserving the colors from the app?

I am calling git via a command line in C#:

var logger = CreateProcess("git", $"log -n 1 {branchName}");
// some more code here
Console.WriteLine(logger.StandardOutput.ReadToEnd());
private static Process CreateProcess(string exe, string args)
{
    var p = new Process();
    p.StartInfo.FileName = exe;
    p.StartInfo.Arguments = args;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    return p;
}

However the colors that are normally displayed by the command line git app are not displayed when I later go to write the redirected output ot the console; everything is white instead. Is there any way to run the app and redirect the output without losing the colors?

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