0

I managed to make a program to run a .bat file and run a console, but I want to get the lines from that console to my program, I searched for it but I couldn't find anything.

The bat will open a console window and I want to get the lines from that console window.

Code :

private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox1.Checked == true)
        {
            Process startM = new Process();
            startM.StartInfo.FileName = @"C:\Users\xxxxx\Desktop\$v\xxxxx\run.bat";
            startM.StartInfo.WorkingDirectory = @"C:\Users\xxxx\Desktop\$v\xxxxx";
            startM.StartInfo.CreateNoWindow = true;
            startM.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startM.Start();
            //startM.WaitForExit();    
            checkBox1.FlatAppearance.BorderColor = Color.Green;
            checkBox1.FlatAppearance.CheckedBackColor = Color.Green;
            checkBox1.Text = "On";
        }

        else if (checkBox1.Checked == false)
        {
            try
            {
                Process[] proc = Process.GetProcessesByName("test");
                proc[0].Kill();
            }
            catch (Exception c)
                {
                    MessageBox.Show(c.Message);
                }
            checkBox1.FlatAppearance.BorderColor = Color.Red;
            checkBox1.Text = "Off";
        }
    }
ZeeDee
  • 19
  • 2
  • 1
    I suggest to add code that show your actual effort. Otherwise your question is too vague and at risk of being closed. – Steve Jan 22 '16 at 23:18
  • 1
    Read [this](https://stackoverflow.com/questions/28931847/using-process-start-to-capture-console-output) for some direction. – Michael Todd Jan 22 '16 at 23:25
  • 2
    http://stackoverflow.com/questions/285760/how-to-spawn-a-process-and-capture-its-stdout-in-net – kamilk Jan 22 '16 at 23:29
  • There is no shortage of existing examples on Stack Overflow of how to read the stdout of a process you've started, including the marked duplicate. Note that the answers to that question focus mainly on the use of the `OutputDataReceived` event, as that's where the OP started. Personally, I prefer using asynchronous reads on the `Out` reader (e.g. `ReadLineAsync()`); I find it cleaner, and I've run across an example where using `OutputDataReceived` doesn't work correctly (due to an apparent bug in .NET or Windows). But either should normally work fine. – Peter Duniho Jan 23 '16 at 00:46

0 Answers0