简体   繁体   English

C#运行没有新窗口的批处理脚本,并将所有(标准/错误)输出重定向到RichTextBox

[英]C# run a batch script without a new window and redirect all (standard/error) output to a RichTextBox

I have a batch script that launches the mariadb in console mode, so it looks likes this: 我有一个在控制台模式下启动mariadb的批处理脚本,因此看起来像这样:

run-mariadb.bat 运行mariadb.bat

@echo off
cd /D mariadb-10.1.14-win32\bin && mysqld.exe --console
@pause

Using a C# WinForm app, I'd like to launch this batch script and get all output onto a RichTextBox. 我想使用C#WinForm应用程序启动此批处理脚本,并将所有输出输出到RichTextBox上。

This is the code I have so far: 这是我到目前为止的代码:

// Configure db server process
Process dbServer = new Process();
dbServer.StartInfo.FileName = "cmd";
dbServer.StartInfo.Arguments = "/c start /wait run-mariadb.bat";
dbServer.StartInfo.CreateNoWindow = true;
dbServer.StartInfo.UseShellExecute = false;
dbServer.StartInfo.RedirectStandardOutput = true;
dbServer.StartInfo.RedirectStandardError = true;
dbServer.OutputDataReceived += new DataReceivedEventHandler(HandleDBServerOutput);
dbServer.ErrorDataReceived += new DataReceivedEventHandler(HandleDBServerOutput);

These are my helper methods: 这些是我的辅助方法:

string FormatOutput(string message)
{
    return string.Format("[ {0} ] : {1}",
        DateTime.Now.ToShortTimeString(),
        message);
}

void HandleDBServerOutput(object sender, DataReceivedEventArgs e)
{
    this.BeginInvoke(new MethodInvoker(() =>
    {
        if (!string.IsNullOrEmpty(e.Data))
            dbLogsRTB.AppendText(FormatOutput(e.Data));
    }));
}

I start the process like this: 我开始这样的过程:

// Start database server
dbServer.Start();
dbServer.BeginOutputReadLine();
dbServer.WaitForExit();

When the above executes, a new command prompt window is created and the mariadb is running in there. 执行上述操作后,将创建一个新的命令提示符窗口,并且mariadb正在其中运行。 Any ideas why dbServer.StartInfo.CreateNoWindow = true; 为什么dbServer.StartInfo.CreateNoWindow = true;任何想法dbServer.StartInfo.CreateNoWindow = true; is being ignored? 被忽略?

The help for start starts off with: start的帮助始于:

Starts a separate window to run a specified program or command. 启动一个单独的窗口以运行指定的程序或命令。

In other words, by running start as part of your command, you'll launch a new window. 换句话说,通过将start作为命令的一部分运行,您将启动一个新窗口。 You can verify this by running start /wait run-mariadb.bat from a command prompt. 您可以通过在命令提示符下运行start /wait run-mariadb.bat来验证这一点。 It launches a new window to run the command. 它会启动一个新窗口来运行命令。

Consider changing your command to be simply: 考虑将命令更改为简单:

dbServer.StartInfo.Arguments = "/c run-mariadb.bat";

To just run the command without starting a new console process. 仅运行命令而不启动新的控制台进程。

暂无
暂无

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

相关问题 在C#中运行程序时,所有消息都转到标准输出,但标准错误不包含任何内容 - When run a program in C#, all the messages go to the standard output, but the standard error contains nothing 使用批处理文件执行的输出实时更新C#表单RichTextBox内容,而无需等待批处理命令执行完成 - Update C# form RichTextBox content (in realtime) with the output of the batch file execution without waiting for the batch command execution to finish 我可以在C#中将流程的标准输出和标准错误与控制台窗口的输出合并吗? - Can I merge the standard output and standard error of a Process with the output of a console window in C#? 在 C# 中重定向 CreateProcessWithTokenW 的标准输出 - Redirect Standard Output of CreateProcessWithTokenW in C# 如何在C#、. net中仅异步重定向标准错误流而不是进程的标准输出流 - How to asynchronously redirect ONLY standard error stream and not standard output stream of a process in C#, .net 在CMD窗口中运行标题为C#的新进程? - Run a new process in a CMD window without the title in C#? 将静默SQL Server安装的输出重定向到C#中的标准错误? - Redirect output from silent SQL Server installation to Standard Error in C#? 从C#中的标准帐户以管理员身份运行批处理文件 - Run a batch file as administrator from an standard account in C# 将Python标准输入/输出重定向到C#表单应用程序 - Redirect Python standard input/output to C# forms application C#:重定向已经运行的进程的标准输出 - C#: Redirect Standard Output of a Process that is Already Running
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM