简体   繁体   English

如何在C#中运行命令行应用程序并重定向/显示其标准输出,同时保留应用程序中的颜色?

[英]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#: 我通过C#中的命令行调用git:

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; 但是,当我稍后去向控制台编写重定向的输出时,通常不会显示命令行git app正常显示的颜色。 everything is white instead. 一切都是白色的。 Is there any way to run the app and redirect the output without losing the colors? 有什么方法可以运行应用程序并重定向输出而不会丢失颜色吗?

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

相关问题 如何在C#中重定向NASM命令行汇编器的输出 - How to redirect output from the NASM command line assembler in C# C# 如何在命令行应用程序中使用异步方法运行线程 - C# How run Threads with Async methods in command line app 如何在C#中显示SQL“PRINT”命令的输出? - How Can i display the output of SQL “PRINT” Command in C#? 将C#应用程序命令行输出写入输入行 - C# app command-line output written to input line 如何在命令运行时从 C# 中的 powershell 获取 output? - How can I get output from powershell in C# while a command is running? 如何在不中断用户输入的情况下在C#Windows控制台应用程序中更新行? - How can I update a line in a C# Windows Console App while without disrupting user input? 如何从 WebClient 或 Url 反序列化 Json 数据并将其显示在 C# 中的标准输出上 - How do I Deserialize Json data from WebClient or Url and display it on standard output in C# 我如何从 C# 运行脚手架命令 - How can i run scaffold command from c# 如何从 c# 运行 VLC 命令 - How can I run VLC command from c# 如何在C#控制台应用程序的行首处允许用户输入? - How can I allow user input from the beginning of the line in C# console app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM