Witajcie,
korzystając z przykładu zawartego tu: https://saezndaree.wordpress.com/2009/03/29/how-to-redirect-the-consoles-output-to-a-textbox-in-c/
udało mi się stworzyć podwaliny dla małej aplikacji która przekazuje wszystko co wpiszemy w pierwszej linii do textbox'a:
ConsoleRedirection.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
namespace ConsoleRedirection
{
public partial class ConsoleRedirection : Form
{
TextWriter _writer = null;
public ConsoleRedirection()
{
InitializeComponent();
}
private void FormConsole_Load(object sender, EventArgs e)
{
_writer = new TextBoxStreamWriter(txtConsoleOutput);
Console.SetOut(_writer);
}
private void txtSayHello_Click(object sender, EventArgs e)
{
Console.WriteLine(txtConsoleInput.Text);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
ConsoleRedirection.Designer.cs
namespace ConsoleRedirection
{
partial class ConsoleRedirection
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.txtConsoleOutput = new System.Windows.Forms.TextBox();
this.txtExecute = new System.Windows.Forms.Button();
this.txtConsoleInput = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// txtConsoleOutput
//
this.txtConsoleOutput.Location = new System.Drawing.Point(12, 41);
this.txtConsoleOutput.Multiline = true;
this.txtConsoleOutput.Name = "txtConsoleOutput";
this.txtConsoleOutput.Size = new System.Drawing.Size(379, 272);
this.txtConsoleOutput.TabIndex = 0;
//
// txtExecute
//
this.txtExecute.Location = new System.Drawing.Point(316, 12);
this.txtExecute.Name = "txtExecute";
this.txtExecute.Size = new System.Drawing.Size(75, 23);
this.txtExecute.TabIndex = 1;
this.txtExecute.Text = "Execute";
this.txtExecute.UseVisualStyleBackColor = true;
this.txtExecute.Click += new System.EventHandler(this.txtSayHello_Click);
//
// txtConsoleInput
//
this.txtConsoleInput.Location = new System.Drawing.Point(12, 12);
this.txtConsoleInput.Name = "txtConsoleInput";
this.txtConsoleInput.Size = new System.Drawing.Size(298, 20);
this.txtConsoleInput.TabIndex = 2;
this.txtConsoleInput.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// ConsoleRedirection
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(403, 325);
this.Controls.Add(this.txtConsoleInput);
this.Controls.Add(this.txtExecute);
this.Controls.Add(this.txtConsoleOutput);
this.Name = "ConsoleRedirection";
this.Text = "ConsoleRedirection";
this.Load += new System.EventHandler(this.FormConsole_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button txtExecute;
private System.Windows.Forms.TextBox txtConsoleOutput;
private System.Windows.Forms.TextBox txtConsoleInput;
}
}
TextBoxStreamWriter.cs
using System;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace ConsoleRedirection
{
public class TextBoxStreamWriter : TextWriter
{
TextBox _output = null;
public TextBoxStreamWriter(TextBox output)
{
_output = output;
}
public override void Write(char value)
{
base.Write(value);
_output.AppendText(value.ToString());
}
public override Encoding Encoding
{
get { return System.Text.Encoding.UTF8; }
}
}
}
Teraz chciałbym aby moja aplikacja uruchomiła konsolę CMD oraz "podłaczyć się" lub "'przechwycić" wejście i wyjście z tej konsoli:
wejście sterowane zawartością pierwszego textbox'a czyli jeżeli wpiszę tam dir i nacisnę przycisk "Execute" to wyjście będzie przekierowane do drugiego texbox'a
Kod który próbuję wykorzystać:
Process process = new Process();
void LaunchProcess()
{
process.EnableRaisingEvents = true;
process.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(process_OutputDataReceived);
process.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(process_ErrorDataReceived);
process.Exited += new System.EventHandler(process_Exited);
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "dir";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
//below line is optional if we want a blocking call
//process.WaitForExit();
}
void process_Exited(object sender, EventArgs e)
{
Console.WriteLine(string.Format("process exited with code {0}\n", process.ExitCode.ToString()));
}
void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data + "\n");
}
void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data + "\n");
}
}
Niestety nie za bardzo rozumiem jak połączyć "Console.SetOut(_writer);" z process_OutputDataReceived + process_ErrorDataReceived
Proszę o pomoc i ew wskazanie czym mam zapełnić tą czarną dziurę w mojej głowie abym rozumiał sposób zaimplementowania tego kodu do moich potrzeb.