2

I need to Copy The Output CMD lines to textbox is that possible ? if yes please Show me Some to know the way to deal with it

enter code here
      private void pictureBox1_Click(object sender, EventArgs e)
       {
        label10.Visible = true;
        string cmd = "/c  adb install BusyBox.apk ";
        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.StartInfo.FileName = "cmd.exe";
        proc.StartInfo.Arguments = cmd;
        proc.StartInfo.RedirectStandardError = true;

        proc.StartInfo.UseShellExecute = false;
        //proc.StartInfo.CreateNoWindow = true;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.Start();

        proc.WaitForExit();
        pictureBox6.Visible = true;
        label10.Text = "Installation Complete";
        // MessageBox.Show("Install Complete ...");
        DateTime Tthen = DateTime.Now;
        do
        {
            Application.DoEvents();
        } while (Tthen.AddSeconds(4) > DateTime.Now);
        label10.Visible = false;
        pictureBox6.Visible = false;

    }
Mostafiz
  • 7,243
  • 3
  • 28
  • 42
  • 1
    Yes, you need to hook the OutputDataReceived and ErrorDataReceived events and write functions that'll trigger an append to your textbox on a UI thread. e.g. see [this old question](http://stackoverflow.com/q/1145969/243245) – Rup May 06 '16 at 10:18
  • Yea it's Works For me but .. not that what I want ... I need to Read it Step by step in exact mean I need to show what's happening now on CMD and past it on Textbot ... The way I made ,is wait the proc. to end and past the Result in text box ... is that possible to do something like this in C# ? – Abdo Hurbly May 06 '16 at 10:28
  • See webpage : https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput(v=vs.110).aspx – jdweng May 06 '16 at 10:37
  • If you hook the events you'll get the data step by step rather than all in one go. – Rup May 06 '16 at 10:49
  • 1
    I would use a `RichTextBox`, not a `TextBox` because it's multiline by default and you can colorize standard Output and Error Output with different colors. – Clijsters May 06 '16 at 13:43

2 Answers2

3

Well you already set up everything as needed, the only thing missing is:

string consoleOutput = proc.StandardOutput.ReadToEnd();
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
1

Use this, then line will contain whole output

proc.Start();
string line = proc.StandardOutput.ReadToEnd();

or for one line

proc.Start();
string line = proc.StandardOutput.ReadLine();

and if you want output line by line then

while (!proc.StandardOutput.EndOfStream) {
    string line = proc.StandardOutput.ReadLine();
    // do your stuff
}

or you can try this one too, first remove proc.WaitForExit(); because ReadLine will wait until data is available or the stream is closed. When the stream is closed, ReadLine will return null.

string line;
while ((line = proc.StandardOutput.ReadLine())!=null) 
{
    // textbox.text = line or something like that
}
Mostafiz
  • 7,243
  • 3
  • 28
  • 42