Search code examples
c#processcmd

opening and writing commands to a cmd from text box


I am trying to open a cmd.exe and write in it from multiple text boxes. But I can't get anything to show up but the cmd:

System.Diagnostics.Process.Start("cmd", "perl "+ textBox5.Text + textBox4.Text + textBox6.Text + textBox7.Text + textBox8.Text + textBox9.Text);

Solution

  • You will need to start either cmd with the option /c and pass every following data by using " like cmd /c "perl ... or you can just start perl as the process and pass everything else as an argument.

    You can find a detailed documentation about the parameters here.

    So you would have to change your code to either

    System.Diagnostics.Process.Start("cmd","/c \"perl "+ textBox5.Text + textBox4.Text + textBox6.Text + textBox7.Text + textBox8.Text + textBox9.Text + "\"");
    

    or

    System.Diagnostics.Process.Start("perl", textBox5.Text + textBox4.Text + textBox6.Text + textBox7.Text + textBox8.Text + textBox9.Text);
    

    In addition: You could improve the readability and performance of your code by NOT using + in combination with strings. If you would use a StringBuilder, you could change your code to the following one:

    StringBuilder arguments = new StringBuilder();
    arguments.Append(textBox5.Text);
    arguments.Append(textBox4.Text);
    arguments.Append(textBox6.Text);
    arguments.Append(textBox7.Text);
    arguments.Append(textBox8.Text);
    arguments.Append(textBox9.Text);
    
    System.Diagnostics.Process.Start("perl", arguments.ToString());