2

I have a problem with my C# project (Windows Form Application). I have two buttons. When I click on the first one, it will open the cmd. Everything works fine.

What I want to reach: Button 2 should use an opened cmd, insert command and collect the output from it. I don't want to open cmd for every command. There will be more buttons with different commands.

Is it possible?

private void button3_Click(object sender, EventArgs e)
        {
            string my_script = textBox3.Text;
            System.Windows.Forms.MessageBox.Show(my_id.ToString());

        }

        public int my_id;
        public void connection_Click(object sender, EventArgs e)
        {

            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.FileName = "cmd.exe";
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo = startInfo;
            process.Start();
            my_id = process.Id;
    }
cotablise
  • 65
  • 6
  • You're executing your SQL statements with the `cmd` window? – Mike Perrenoud Apr 10 '13 at 15:15
  • what type of command do you want to run? – KF2 Apr 10 '13 at 15:16
  • Take a look here http://stackoverflow.com/questions/4587415/how-to-capture-shell-command-output-in-c – NoWar Apr 10 '13 at 15:16
  • Search for "capturing console output" and you'll find a bunch of solutions – Z . Apr 10 '13 at 15:16
  • I dont want use it for SQL statements. I would like to add several buttons. Each of them should run different command. For example button 2 will run command "systeminfo", button 3 will run command "hostname" and so on... – cotablise Apr 10 '13 at 15:22

2 Answers2

3

You can use process.StandardInput to send commands to executing cmd.exe, and process.StandardOutput to read the results.

alex
  • 12,464
  • 3
  • 46
  • 67
  • problem is that I don't want to start cmd with every button. I wanted to open it once and other buttons should send only command into this opened console. – cotablise Apr 10 '13 at 15:28
  • You don't need to start it on every button click. Open it first time with /K parameter, so it would not close by itself. And then write commands to process.StandardInput. It's like typing them directly into cmd window. – alex Apr 10 '13 at 15:33
  • sorry, I am beginner in the programming. How can I do it? I will try to add whole code what I have, give me a second – cotablise Apr 10 '13 at 15:38
  • You can create a field `process` in your class, assign it to new process once, and then write data to process.StandardInput from the button handlers. – alex Apr 10 '13 at 15:42
  • ok thank you very much, I will try it, but I am not sure whether I will be successful, anyway thank you very much for now – cotablise Apr 10 '13 at 15:44
  • If you find a minute, please check my new post. Thank you in advance. – cotablise Apr 10 '13 at 17:53
2

Maybe you should better embed a console in your app, that trying to communicate with an external console.

Read this : http://www.codeproject.com/Articles/335909/Embedding-a-Console-in-a-C-Application

Steve B
  • 36,818
  • 21
  • 101
  • 174
  • hmmm interesting, I will have to read it deeper. It is no important for me to see the console. Commands can run on the background. If there will not be nothing easier , this could also help. I will let you know. Thank you for your reply. – cotablise Apr 10 '13 at 15:25
  • That said, most of time, [Alex's answer](http://stackoverflow.com/a/15929927/588868) will suit such requirment. You should really explain what kind of command you have to run to help us understand what is your goal. – Steve B Apr 10 '13 at 15:29
  • click on Button1 - cmd will be opened; click on Button2 - command "systeminfo" will be executed in opened cmd; click on Button3 - command "hostname" will be executed in opened cmd – cotablise Apr 10 '13 at 15:31