从批处理文件接收输出并输入
Receive output from a batch file and input
我目前正在用 C# 创建一个应用程序,它允许您创建服务器并轻松管理它,为此它使用批处理文件来 运行 所述服务器。它通过创建批处理文件并将其用于 运行 服务器来工作。 (Java 顺便说一下)。
所以,我想知道是否可以从控制台获取输出,而不是像下面的代码那样将其转储到文本框中并关闭控制台,我需要它不断 post 控制台的任何输出都没有关闭,也没有垃圾邮件负载打开控制台(尝试使用计时器)。
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo =
new ProcessStartInfo(batchfilelocation);
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
StreamReader myStreamReader = myProcess.StandardOutput;
// Read the standard output of the spawned process.
string myString = myStreamReader.ReadToEnd();
textBox1.Text = textBox1.Text + myString;
myProcess.Close();
我还想知道是否可以允许来自 windows 表单控件的输入,例如通过在控制台中输入命令并按回车键来执行命令的按钮。或者允许您执行相同操作的文本框。
谢谢!
您不需要 TextBox 控件来存储字符串。只需使用字符串变量即可。
string concatenatedString = null;
concatenatedString += myStreamReader.ReadToEnd();
其次,您可以在不创建控制台的情况下执行此操作window。下面的代码将允许您的程序 运行 而不会看到大量控制台 windows 弹出窗口。
myProcessStartInfo.CreateNoWindow = true;
我目前正在用 C# 创建一个应用程序,它允许您创建服务器并轻松管理它,为此它使用批处理文件来 运行 所述服务器。它通过创建批处理文件并将其用于 运行 服务器来工作。 (Java 顺便说一下)。
所以,我想知道是否可以从控制台获取输出,而不是像下面的代码那样将其转储到文本框中并关闭控制台,我需要它不断 post 控制台的任何输出都没有关闭,也没有垃圾邮件负载打开控制台(尝试使用计时器)。
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo =
new ProcessStartInfo(batchfilelocation);
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
StreamReader myStreamReader = myProcess.StandardOutput;
// Read the standard output of the spawned process.
string myString = myStreamReader.ReadToEnd();
textBox1.Text = textBox1.Text + myString;
myProcess.Close();
我还想知道是否可以允许来自 windows 表单控件的输入,例如通过在控制台中输入命令并按回车键来执行命令的按钮。或者允许您执行相同操作的文本框。
谢谢!
您不需要 TextBox 控件来存储字符串。只需使用字符串变量即可。
string concatenatedString = null;
concatenatedString += myStreamReader.ReadToEnd();
其次,您可以在不创建控制台的情况下执行此操作window。下面的代码将允许您的程序 运行 而不会看到大量控制台 windows 弹出窗口。
myProcessStartInfo.CreateNoWindow = true;