I've got a problem with showing results of cmd commands to richtextbox. So far, I have this code:
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);
}
I've tried replacing "Console.WriteLine(e.Data);" with "richTextBox1.Text = richTextBox1.Text + (e.Data);" but it wouldn't be accepted. I also tried "richTextBox1.Text = richTextBox1.Text + cmd_DataReceived"; But that didn't work iether. Then I tried replacing console.writeline with messagebox.show(e.data) but again.. nope The commands work, but it won't display
I know I copied most of the code, and it's probably meant for a console application.
Please help