输入cmd命令,显示结果到richtextbox c#
Type cmd command, display result to richtextbox c#
我在向 richtextbox 显示 cmd 命令的结果时遇到了问题。
到目前为止,我有这个代码:
private void richTextBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
e.SuppressKeyPress = true;
ProcessStartInfo cmdStartInfo = new ProcessStartInfo();
cmdStartInfo.FileName = @"C:\Windows\System32\cmd.exe";
cmdStartInfo.RedirectStandardOutput = true;
cmdStartInfo.RedirectStandardError = true;
cmdStartInfo.RedirectStandardInput = true;
cmdStartInfo.UseShellExecute = false;
cmdStartInfo.CreateNoWindow = true;
Process cmdProcess = new Process();
cmdProcess.StartInfo = cmdStartInfo;
cmdProcess.OutputDataReceived += cmd_DataReceived;
cmdProcess.EnableRaisingEvents = true;
cmdProcess.Start();
cmdProcess.BeginOutputReadLine();
cmdProcess.BeginErrorReadLine();
cmdProcess.StandardInput.WriteLine(richTextBox2.Text);
cmdProcess.StandardInput.WriteLine("exit");
cmdProcess.WaitForExit();
richTextBox1.Text = richTextBox1.Text + cmd_DataReceived + Environment.NewLine;
}
}
static void cmd_DataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine("Output from other process");
Console.WriteLine(e.Data);
}
我试过用 "richTextBox1.Text = richTextBox1.Text + (e.Data);" 替换 "Console.WriteLine(e.Data);" 但它不会被接受。
我也试过 "richTextBox1.Text = richTextBox1.Text + cmd_DataReceived";
但这没有用。
然后我尝试用 messagebox.show(e.data) 替换 console.writeline 但又一次..不
命令有效,但不会显示
我知道我复制了大部分代码,它可能适用于控制台应用程序。
请帮忙
当您等待进程以 cmdProcess.WaitForExit
退出时,cmd_DataReceived
正在获取所有数据。这意味着您将需要存储从回调中获得的数据,并且在 WaitForExit
结束后,您可以使用存储数据的内容设置文本框。
一个简单的方法是使用 StringBuilder
:
// Have a reference to a stringbuilder that both functions can see
StringBuilder m_output;
private void richTextBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// Before the process starts, create a new stringbuilder
m_output = new StringBuilder();
e.Handled = true;
e.SuppressKeyPress = true;
ProcessStartInfo cmdStartInfo = new ProcessStartInfo();
cmdStartInfo.FileName = @"C:\Windows\System32\cmd.exe";
cmdStartInfo.RedirectStandardOutput = true;
cmdStartInfo.RedirectStandardError = true;
cmdStartInfo.RedirectStandardInput = true;
cmdStartInfo.UseShellExecute = false;
cmdStartInfo.CreateNoWindow = true;
Process cmdProcess = new Process();
cmdProcess.StartInfo = cmdStartInfo;
cmdProcess.OutputDataReceived += cmd_DataReceived;
cmdProcess.EnableRaisingEvents = true;
cmdProcess.Start();
cmdProcess.BeginOutputReadLine();
cmdProcess.BeginErrorReadLine();
cmdProcess.StandardInput.WriteLine(richTextBox2.Text);
cmdProcess.StandardInput.WriteLine("exit");
cmdProcess.WaitForExit();
// And now that everything's done, just set the text
// to whatever's in the stringbuilder
richTextBox1.Text = m_output.ToString();
// We're done with the stringbuilder, let the garbage
// collector free it
m_output = null;
}
}
// Note: This is no longer a static method so it has
// access to the member variables, including m_output
void cmd_DataReceived(object sender, DataReceivedEventArgs e)
{
Debug.WriteLine("Output from other process");
Debug.WriteLine(e.Data);
// Add the data, one line at a time, to the string builder
m_output.AppendLine(e.Data);
}
我在向 richtextbox 显示 cmd 命令的结果时遇到了问题。 到目前为止,我有这个代码:
private void richTextBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
e.SuppressKeyPress = true;
ProcessStartInfo cmdStartInfo = new ProcessStartInfo();
cmdStartInfo.FileName = @"C:\Windows\System32\cmd.exe";
cmdStartInfo.RedirectStandardOutput = true;
cmdStartInfo.RedirectStandardError = true;
cmdStartInfo.RedirectStandardInput = true;
cmdStartInfo.UseShellExecute = false;
cmdStartInfo.CreateNoWindow = true;
Process cmdProcess = new Process();
cmdProcess.StartInfo = cmdStartInfo;
cmdProcess.OutputDataReceived += cmd_DataReceived;
cmdProcess.EnableRaisingEvents = true;
cmdProcess.Start();
cmdProcess.BeginOutputReadLine();
cmdProcess.BeginErrorReadLine();
cmdProcess.StandardInput.WriteLine(richTextBox2.Text);
cmdProcess.StandardInput.WriteLine("exit");
cmdProcess.WaitForExit();
richTextBox1.Text = richTextBox1.Text + cmd_DataReceived + Environment.NewLine;
}
}
static void cmd_DataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine("Output from other process");
Console.WriteLine(e.Data);
}
我试过用 "richTextBox1.Text = richTextBox1.Text + (e.Data);" 替换 "Console.WriteLine(e.Data);" 但它不会被接受。 我也试过 "richTextBox1.Text = richTextBox1.Text + cmd_DataReceived"; 但这没有用。 然后我尝试用 messagebox.show(e.data) 替换 console.writeline 但又一次..不 命令有效,但不会显示
我知道我复制了大部分代码,它可能适用于控制台应用程序。
请帮忙
当您等待进程以 cmdProcess.WaitForExit
退出时,cmd_DataReceived
正在获取所有数据。这意味着您将需要存储从回调中获得的数据,并且在 WaitForExit
结束后,您可以使用存储数据的内容设置文本框。
一个简单的方法是使用 StringBuilder
:
// Have a reference to a stringbuilder that both functions can see
StringBuilder m_output;
private void richTextBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// Before the process starts, create a new stringbuilder
m_output = new StringBuilder();
e.Handled = true;
e.SuppressKeyPress = true;
ProcessStartInfo cmdStartInfo = new ProcessStartInfo();
cmdStartInfo.FileName = @"C:\Windows\System32\cmd.exe";
cmdStartInfo.RedirectStandardOutput = true;
cmdStartInfo.RedirectStandardError = true;
cmdStartInfo.RedirectStandardInput = true;
cmdStartInfo.UseShellExecute = false;
cmdStartInfo.CreateNoWindow = true;
Process cmdProcess = new Process();
cmdProcess.StartInfo = cmdStartInfo;
cmdProcess.OutputDataReceived += cmd_DataReceived;
cmdProcess.EnableRaisingEvents = true;
cmdProcess.Start();
cmdProcess.BeginOutputReadLine();
cmdProcess.BeginErrorReadLine();
cmdProcess.StandardInput.WriteLine(richTextBox2.Text);
cmdProcess.StandardInput.WriteLine("exit");
cmdProcess.WaitForExit();
// And now that everything's done, just set the text
// to whatever's in the stringbuilder
richTextBox1.Text = m_output.ToString();
// We're done with the stringbuilder, let the garbage
// collector free it
m_output = null;
}
}
// Note: This is no longer a static method so it has
// access to the member variables, including m_output
void cmd_DataReceived(object sender, DataReceivedEventArgs e)
{
Debug.WriteLine("Output from other process");
Debug.WriteLine(e.Data);
// Add the data, one line at a time, to the string builder
m_output.AppendLine(e.Data);
}