I've been looking at similar posts to mine, but either my question is not answered, or I cannot understand the answer. I have used many code snippets off here to get started; so thanks for that ;-)
My requirement is to create an interactive cmd.exe window, and output results to text box ( to parse data ) eg, send command "cd /"; see the output then send command "dir" to show that I am in the new directory. Once I have this cracked, then I plan to parse the received text output and expand my application
I can currently do both things, but not at the same time.
I am new to c#, and have been stuck on this for a few days now. Code posted below.
proc1 manages to keep the session active, whereas proc2 outputs the text to a text box (I'll later on out it into a string to parse); but I can't manage to do both requirements at the same time.
I can explain more why I want to do this, but ultimately to create a concept application to expand once I have the basics cracked...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace ConsoleTest_07_07_14
{
public partial class Form1 : Form
{
Process proc1 = new Process();
Process proc2 = new Process();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SetProc1();
proc1.Start();
SetProc2();
proc2.Start();
}
public void SetProc1()
{
proc1.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";
proc1.StartInfo.WorkingDirectory = @"C:\Windows";
proc1.StartInfo.UseShellExecute = false;
proc1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc2.StartInfo.CreateNoWindow = true;
proc1.StartInfo.RedirectStandardInput = true;
// proc2.StartInfo.RedirectStandardOutput = true;
}
public void SetProc2()
{
proc2.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";
proc2.StartInfo.WorkingDirectory = @"C:\Windows";
proc2.StartInfo.UseShellExecute = false;
proc2.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc2.StartInfo.CreateNoWindow = true;
proc2.StartInfo.RedirectStandardInput = true;
proc2.StartInfo.RedirectStandardOutput = true;
}
private void button1_Click(object sender, EventArgs e)
{
// process1
StreamWriter SW1 = proc1.StandardInput;
SW1.WriteLine(textBox3.Text);
}
private void button2_Click(object sender, EventArgs e)
{
proc2.StartInfo.Arguments = "/c dir";
proc2.Start();
StreamReader SR2 = proc2.StandardOutput;
textBox2.Text = SR2.ReadToEnd();
}
}
}