Console within form Visual C#





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







3















I want to get a console window within my form. Basically when you click button1, it runs a batch script(test.exe). I don't want a separate batch window but instead I want it to show up within my form.



I figure there are probably two ways of doing this, either 1, somehow embedding the console within my form, or 2, set StartInfo.CreateNoWindow = true; when you click button1 and get the output to funnel into a listbox to simulate a console within my form.



I am just a little stuck because I have found methods for doing both but my own testing with the various other methods people have suggested, nothing has worked. But either way, my user needs to be able to send input back to the console.



Which method would be simpler and how would I go about it?










share|improve this question




















  • 4





    something like this?

    – JNevill
    Nov 23 '18 at 17:08











  • or something like this?

    – Koder101
    Nov 23 '18 at 17:36













  • @JNevill Yes but I am unable to get it to even work with a completely fresh project. Unless I am not understanding something, half of the setup code the guy gives is just variables that are either not defined anywhere or code that makes no sense as to what it is supposed to relate to.

    – Matt Lynch
    Nov 23 '18 at 19:39











  • @Koder101 no, that is not what im looking for

    – Matt Lynch
    Nov 23 '18 at 19:40











  • He's got the entire source code, binaries, and sample application there (I haven't looked at it though, so you might be right). It seems like a rather straight forward library you can install through nuget.

    – JNevill
    Nov 23 '18 at 19:44


















3















I want to get a console window within my form. Basically when you click button1, it runs a batch script(test.exe). I don't want a separate batch window but instead I want it to show up within my form.



I figure there are probably two ways of doing this, either 1, somehow embedding the console within my form, or 2, set StartInfo.CreateNoWindow = true; when you click button1 and get the output to funnel into a listbox to simulate a console within my form.



I am just a little stuck because I have found methods for doing both but my own testing with the various other methods people have suggested, nothing has worked. But either way, my user needs to be able to send input back to the console.



Which method would be simpler and how would I go about it?










share|improve this question




















  • 4





    something like this?

    – JNevill
    Nov 23 '18 at 17:08











  • or something like this?

    – Koder101
    Nov 23 '18 at 17:36













  • @JNevill Yes but I am unable to get it to even work with a completely fresh project. Unless I am not understanding something, half of the setup code the guy gives is just variables that are either not defined anywhere or code that makes no sense as to what it is supposed to relate to.

    – Matt Lynch
    Nov 23 '18 at 19:39











  • @Koder101 no, that is not what im looking for

    – Matt Lynch
    Nov 23 '18 at 19:40











  • He's got the entire source code, binaries, and sample application there (I haven't looked at it though, so you might be right). It seems like a rather straight forward library you can install through nuget.

    – JNevill
    Nov 23 '18 at 19:44














3












3








3


3






I want to get a console window within my form. Basically when you click button1, it runs a batch script(test.exe). I don't want a separate batch window but instead I want it to show up within my form.



I figure there are probably two ways of doing this, either 1, somehow embedding the console within my form, or 2, set StartInfo.CreateNoWindow = true; when you click button1 and get the output to funnel into a listbox to simulate a console within my form.



I am just a little stuck because I have found methods for doing both but my own testing with the various other methods people have suggested, nothing has worked. But either way, my user needs to be able to send input back to the console.



Which method would be simpler and how would I go about it?










share|improve this question
















I want to get a console window within my form. Basically when you click button1, it runs a batch script(test.exe). I don't want a separate batch window but instead I want it to show up within my form.



I figure there are probably two ways of doing this, either 1, somehow embedding the console within my form, or 2, set StartInfo.CreateNoWindow = true; when you click button1 and get the output to funnel into a listbox to simulate a console within my form.



I am just a little stuck because I have found methods for doing both but my own testing with the various other methods people have suggested, nothing has worked. But either way, my user needs to be able to send input back to the console.



Which method would be simpler and how would I go about it?







c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 26 at 18:00









Alexander I.

9932825




9932825










asked Nov 23 '18 at 17:06









Matt LynchMatt Lynch

625




625








  • 4





    something like this?

    – JNevill
    Nov 23 '18 at 17:08











  • or something like this?

    – Koder101
    Nov 23 '18 at 17:36













  • @JNevill Yes but I am unable to get it to even work with a completely fresh project. Unless I am not understanding something, half of the setup code the guy gives is just variables that are either not defined anywhere or code that makes no sense as to what it is supposed to relate to.

    – Matt Lynch
    Nov 23 '18 at 19:39











  • @Koder101 no, that is not what im looking for

    – Matt Lynch
    Nov 23 '18 at 19:40











  • He's got the entire source code, binaries, and sample application there (I haven't looked at it though, so you might be right). It seems like a rather straight forward library you can install through nuget.

    – JNevill
    Nov 23 '18 at 19:44














  • 4





    something like this?

    – JNevill
    Nov 23 '18 at 17:08











  • or something like this?

    – Koder101
    Nov 23 '18 at 17:36













  • @JNevill Yes but I am unable to get it to even work with a completely fresh project. Unless I am not understanding something, half of the setup code the guy gives is just variables that are either not defined anywhere or code that makes no sense as to what it is supposed to relate to.

    – Matt Lynch
    Nov 23 '18 at 19:39











  • @Koder101 no, that is not what im looking for

    – Matt Lynch
    Nov 23 '18 at 19:40











  • He's got the entire source code, binaries, and sample application there (I haven't looked at it though, so you might be right). It seems like a rather straight forward library you can install through nuget.

    – JNevill
    Nov 23 '18 at 19:44








4




4





something like this?

– JNevill
Nov 23 '18 at 17:08





something like this?

– JNevill
Nov 23 '18 at 17:08













or something like this?

– Koder101
Nov 23 '18 at 17:36







or something like this?

– Koder101
Nov 23 '18 at 17:36















@JNevill Yes but I am unable to get it to even work with a completely fresh project. Unless I am not understanding something, half of the setup code the guy gives is just variables that are either not defined anywhere or code that makes no sense as to what it is supposed to relate to.

– Matt Lynch
Nov 23 '18 at 19:39





@JNevill Yes but I am unable to get it to even work with a completely fresh project. Unless I am not understanding something, half of the setup code the guy gives is just variables that are either not defined anywhere or code that makes no sense as to what it is supposed to relate to.

– Matt Lynch
Nov 23 '18 at 19:39













@Koder101 no, that is not what im looking for

– Matt Lynch
Nov 23 '18 at 19:40





@Koder101 no, that is not what im looking for

– Matt Lynch
Nov 23 '18 at 19:40













He's got the entire source code, binaries, and sample application there (I haven't looked at it though, so you might be right). It seems like a rather straight forward library you can install through nuget.

– JNevill
Nov 23 '18 at 19:44





He's got the entire source code, binaries, and sample application there (I haven't looked at it though, so you might be right). It seems like a rather straight forward library you can install through nuget.

– JNevill
Nov 23 '18 at 19:44












2 Answers
2






active

oldest

votes


















2














I believe the best way to do this is to redirect output. Basically things will still execute as you want, but you will get the output wherever you want/need.






share|improve this answer































    0














    using System;
    using System.Diagnostics;
    using System.Text;
    using System.Windows.Forms;

    namespace ConsoleOutput_test
    {
    public partial class Form1 : Form
    {
    Process sortProcess;
    private static StringBuilder sortOutput = null;

    public Form1()
    {
    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
    sortProcess = new Process();
    sortProcess.StartInfo.FileName = "C:\Windows\System32\cmd.exe";
    // Set UseShellExecute to false for redirection.
    sortProcess.StartInfo.CreateNoWindow = true;
    sortProcess.StartInfo.UseShellExecute = false;

    // Redirect the standard output of the sort command.
    // This stream is read asynchronously using an event handler.
    sortProcess.StartInfo.RedirectStandardOutput = true;
    sortProcess.StartInfo.RedirectStandardInput = true;
    sortProcess.StartInfo.RedirectStandardError = true;
    sortOutput = new StringBuilder("");

    // Set our event handler to asynchronously read the sort output.
    sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
    sortProcess.ErrorDataReceived += new DataReceivedEventHandler(SortErrorHandler);

    // Redirect standard input as well. This stream
    // is used synchronously.
    sortProcess.StartInfo.RedirectStandardInput = true;

    // Start the process.
    sortProcess.Start();

    // Start the asynchronous read of the sort output stream.
    sortProcess.BeginOutputReadLine();
    while (!sortProcess.HasExited)
    {
    Application.DoEvents(); // This keeps your form responsive by processing events
    }
    }

    private void SortOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
    {
    if (txtConsole.InvokeRequired) { txtConsole.BeginInvoke(new DataReceivedEventHandler(SortOutputHandler), new { sendingProcess, outLine }); }
    else
    {
    txtConsole.AppendText(Environment.NewLine + outLine.Data);
    }
    }
    private void SortErrorHandler(object sendingProcess, DataReceivedEventArgs outLine)
    {
    if (txtConsole.InvokeRequired) { txtConsole.BeginInvoke(new DataReceivedEventHandler(SortErrorHandler), new { sendingProcess, outLine }); }
    else
    {
    txtConsole.AppendText(Environment.NewLine + outLine.Data);
    }
    }


    private void button2_Click(object sender, EventArgs e)
    {
    sortProcess.StandardInput.WriteLine(txtOutput.Text);
    txtOutput.Text = "";
    }
    }
    }





    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450605%2fconsole-within-form-visual-c-sharp%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      I believe the best way to do this is to redirect output. Basically things will still execute as you want, but you will get the output wherever you want/need.






      share|improve this answer




























        2














        I believe the best way to do this is to redirect output. Basically things will still execute as you want, but you will get the output wherever you want/need.






        share|improve this answer


























          2












          2








          2







          I believe the best way to do this is to redirect output. Basically things will still execute as you want, but you will get the output wherever you want/need.






          share|improve this answer













          I believe the best way to do this is to redirect output. Basically things will still execute as you want, but you will get the output wherever you want/need.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 17:22









          LeonardoLeonardo

          4,41053590




          4,41053590

























              0














              using System;
              using System.Diagnostics;
              using System.Text;
              using System.Windows.Forms;

              namespace ConsoleOutput_test
              {
              public partial class Form1 : Form
              {
              Process sortProcess;
              private static StringBuilder sortOutput = null;

              public Form1()
              {
              InitializeComponent();
              }

              private void button1_Click(object sender, EventArgs e)
              {
              sortProcess = new Process();
              sortProcess.StartInfo.FileName = "C:\Windows\System32\cmd.exe";
              // Set UseShellExecute to false for redirection.
              sortProcess.StartInfo.CreateNoWindow = true;
              sortProcess.StartInfo.UseShellExecute = false;

              // Redirect the standard output of the sort command.
              // This stream is read asynchronously using an event handler.
              sortProcess.StartInfo.RedirectStandardOutput = true;
              sortProcess.StartInfo.RedirectStandardInput = true;
              sortProcess.StartInfo.RedirectStandardError = true;
              sortOutput = new StringBuilder("");

              // Set our event handler to asynchronously read the sort output.
              sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
              sortProcess.ErrorDataReceived += new DataReceivedEventHandler(SortErrorHandler);

              // Redirect standard input as well. This stream
              // is used synchronously.
              sortProcess.StartInfo.RedirectStandardInput = true;

              // Start the process.
              sortProcess.Start();

              // Start the asynchronous read of the sort output stream.
              sortProcess.BeginOutputReadLine();
              while (!sortProcess.HasExited)
              {
              Application.DoEvents(); // This keeps your form responsive by processing events
              }
              }

              private void SortOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
              {
              if (txtConsole.InvokeRequired) { txtConsole.BeginInvoke(new DataReceivedEventHandler(SortOutputHandler), new { sendingProcess, outLine }); }
              else
              {
              txtConsole.AppendText(Environment.NewLine + outLine.Data);
              }
              }
              private void SortErrorHandler(object sendingProcess, DataReceivedEventArgs outLine)
              {
              if (txtConsole.InvokeRequired) { txtConsole.BeginInvoke(new DataReceivedEventHandler(SortErrorHandler), new { sendingProcess, outLine }); }
              else
              {
              txtConsole.AppendText(Environment.NewLine + outLine.Data);
              }
              }


              private void button2_Click(object sender, EventArgs e)
              {
              sortProcess.StandardInput.WriteLine(txtOutput.Text);
              txtOutput.Text = "";
              }
              }
              }





              share|improve this answer




























                0














                using System;
                using System.Diagnostics;
                using System.Text;
                using System.Windows.Forms;

                namespace ConsoleOutput_test
                {
                public partial class Form1 : Form
                {
                Process sortProcess;
                private static StringBuilder sortOutput = null;

                public Form1()
                {
                InitializeComponent();
                }

                private void button1_Click(object sender, EventArgs e)
                {
                sortProcess = new Process();
                sortProcess.StartInfo.FileName = "C:\Windows\System32\cmd.exe";
                // Set UseShellExecute to false for redirection.
                sortProcess.StartInfo.CreateNoWindow = true;
                sortProcess.StartInfo.UseShellExecute = false;

                // Redirect the standard output of the sort command.
                // This stream is read asynchronously using an event handler.
                sortProcess.StartInfo.RedirectStandardOutput = true;
                sortProcess.StartInfo.RedirectStandardInput = true;
                sortProcess.StartInfo.RedirectStandardError = true;
                sortOutput = new StringBuilder("");

                // Set our event handler to asynchronously read the sort output.
                sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
                sortProcess.ErrorDataReceived += new DataReceivedEventHandler(SortErrorHandler);

                // Redirect standard input as well. This stream
                // is used synchronously.
                sortProcess.StartInfo.RedirectStandardInput = true;

                // Start the process.
                sortProcess.Start();

                // Start the asynchronous read of the sort output stream.
                sortProcess.BeginOutputReadLine();
                while (!sortProcess.HasExited)
                {
                Application.DoEvents(); // This keeps your form responsive by processing events
                }
                }

                private void SortOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
                {
                if (txtConsole.InvokeRequired) { txtConsole.BeginInvoke(new DataReceivedEventHandler(SortOutputHandler), new { sendingProcess, outLine }); }
                else
                {
                txtConsole.AppendText(Environment.NewLine + outLine.Data);
                }
                }
                private void SortErrorHandler(object sendingProcess, DataReceivedEventArgs outLine)
                {
                if (txtConsole.InvokeRequired) { txtConsole.BeginInvoke(new DataReceivedEventHandler(SortErrorHandler), new { sendingProcess, outLine }); }
                else
                {
                txtConsole.AppendText(Environment.NewLine + outLine.Data);
                }
                }


                private void button2_Click(object sender, EventArgs e)
                {
                sortProcess.StandardInput.WriteLine(txtOutput.Text);
                txtOutput.Text = "";
                }
                }
                }





                share|improve this answer


























                  0












                  0








                  0







                  using System;
                  using System.Diagnostics;
                  using System.Text;
                  using System.Windows.Forms;

                  namespace ConsoleOutput_test
                  {
                  public partial class Form1 : Form
                  {
                  Process sortProcess;
                  private static StringBuilder sortOutput = null;

                  public Form1()
                  {
                  InitializeComponent();
                  }

                  private void button1_Click(object sender, EventArgs e)
                  {
                  sortProcess = new Process();
                  sortProcess.StartInfo.FileName = "C:\Windows\System32\cmd.exe";
                  // Set UseShellExecute to false for redirection.
                  sortProcess.StartInfo.CreateNoWindow = true;
                  sortProcess.StartInfo.UseShellExecute = false;

                  // Redirect the standard output of the sort command.
                  // This stream is read asynchronously using an event handler.
                  sortProcess.StartInfo.RedirectStandardOutput = true;
                  sortProcess.StartInfo.RedirectStandardInput = true;
                  sortProcess.StartInfo.RedirectStandardError = true;
                  sortOutput = new StringBuilder("");

                  // Set our event handler to asynchronously read the sort output.
                  sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
                  sortProcess.ErrorDataReceived += new DataReceivedEventHandler(SortErrorHandler);

                  // Redirect standard input as well. This stream
                  // is used synchronously.
                  sortProcess.StartInfo.RedirectStandardInput = true;

                  // Start the process.
                  sortProcess.Start();

                  // Start the asynchronous read of the sort output stream.
                  sortProcess.BeginOutputReadLine();
                  while (!sortProcess.HasExited)
                  {
                  Application.DoEvents(); // This keeps your form responsive by processing events
                  }
                  }

                  private void SortOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
                  {
                  if (txtConsole.InvokeRequired) { txtConsole.BeginInvoke(new DataReceivedEventHandler(SortOutputHandler), new { sendingProcess, outLine }); }
                  else
                  {
                  txtConsole.AppendText(Environment.NewLine + outLine.Data);
                  }
                  }
                  private void SortErrorHandler(object sendingProcess, DataReceivedEventArgs outLine)
                  {
                  if (txtConsole.InvokeRequired) { txtConsole.BeginInvoke(new DataReceivedEventHandler(SortErrorHandler), new { sendingProcess, outLine }); }
                  else
                  {
                  txtConsole.AppendText(Environment.NewLine + outLine.Data);
                  }
                  }


                  private void button2_Click(object sender, EventArgs e)
                  {
                  sortProcess.StandardInput.WriteLine(txtOutput.Text);
                  txtOutput.Text = "";
                  }
                  }
                  }





                  share|improve this answer













                  using System;
                  using System.Diagnostics;
                  using System.Text;
                  using System.Windows.Forms;

                  namespace ConsoleOutput_test
                  {
                  public partial class Form1 : Form
                  {
                  Process sortProcess;
                  private static StringBuilder sortOutput = null;

                  public Form1()
                  {
                  InitializeComponent();
                  }

                  private void button1_Click(object sender, EventArgs e)
                  {
                  sortProcess = new Process();
                  sortProcess.StartInfo.FileName = "C:\Windows\System32\cmd.exe";
                  // Set UseShellExecute to false for redirection.
                  sortProcess.StartInfo.CreateNoWindow = true;
                  sortProcess.StartInfo.UseShellExecute = false;

                  // Redirect the standard output of the sort command.
                  // This stream is read asynchronously using an event handler.
                  sortProcess.StartInfo.RedirectStandardOutput = true;
                  sortProcess.StartInfo.RedirectStandardInput = true;
                  sortProcess.StartInfo.RedirectStandardError = true;
                  sortOutput = new StringBuilder("");

                  // Set our event handler to asynchronously read the sort output.
                  sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
                  sortProcess.ErrorDataReceived += new DataReceivedEventHandler(SortErrorHandler);

                  // Redirect standard input as well. This stream
                  // is used synchronously.
                  sortProcess.StartInfo.RedirectStandardInput = true;

                  // Start the process.
                  sortProcess.Start();

                  // Start the asynchronous read of the sort output stream.
                  sortProcess.BeginOutputReadLine();
                  while (!sortProcess.HasExited)
                  {
                  Application.DoEvents(); // This keeps your form responsive by processing events
                  }
                  }

                  private void SortOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
                  {
                  if (txtConsole.InvokeRequired) { txtConsole.BeginInvoke(new DataReceivedEventHandler(SortOutputHandler), new { sendingProcess, outLine }); }
                  else
                  {
                  txtConsole.AppendText(Environment.NewLine + outLine.Data);
                  }
                  }
                  private void SortErrorHandler(object sendingProcess, DataReceivedEventArgs outLine)
                  {
                  if (txtConsole.InvokeRequired) { txtConsole.BeginInvoke(new DataReceivedEventHandler(SortErrorHandler), new { sendingProcess, outLine }); }
                  else
                  {
                  txtConsole.AppendText(Environment.NewLine + outLine.Data);
                  }
                  }


                  private void button2_Click(object sender, EventArgs e)
                  {
                  sortProcess.StandardInput.WriteLine(txtOutput.Text);
                  txtOutput.Text = "";
                  }
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 6 '18 at 15:33









                  Matt LynchMatt Lynch

                  625




                  625






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450605%2fconsole-within-form-visual-c-sharp%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      RAC Tourist Trophy