1

i would like to do an automated test system which will allow me to run a batch file automatically. right now the procedure is:

  1. run cmd.exe
  2. type in "antc"

i would like to have a button so that once the user clicks it, the above processes are ran automatically.

i have something done, which allows me to open up cmd.exe as shown below:

protected void Button1_Click(object sender, EventArgs e)
{

    System.Diagnostics.Process process1 = new System.Diagnostics.Process();
    process1.StartInfo.WorkingDirectory = Request.MapPath("~/");
    process1.StartInfo.FileName = Request.MapPath("CMD.EXE"); 
    process1.Start(); 
}

thanks and regards

jeremychan
  • 4,309
  • 10
  • 42
  • 56

2 Answers2

1

What's the question? Also you can simply start the batch file directly, no need to start CMD.EXE first. If you need to make the user press a key before closing the window, end your batch file with the PAUSE command.

EDIT: Sorry I did not notice the "web form" part. So Now my question is: What do you want happen? You will run the batch from on the server from a web form. But do you want to display anything to the web browser? What exactly do you want to happen?

EDIT2:

Here is code I have which does what you need:

Process proc = new Process();

proc.StartInfo.FileName = "c:\\whatever\\executable.exe";
proc.StartInfo.Arguments = "-parameter -parameter -etc";
proc.StartInfo.UseShellExecute = false; // You may or may not need this

// For sure you need this
proc.StartInfo.RedirectStandardOutput = true;

// You may not need this
proc.StartInfo.RedirectStandardError = true;

proc.Start();

// For sure you need this
string procOutput = proc.StandardOutput.ReadToEnd();

// You may not need this
string procError = proc.StandardError.ReadToEnd();

At this point, procOutput contains the full console output of the process (batch file).

Gabriel Magana
  • 4,338
  • 24
  • 23
  • the webform i create has several fields for user to input. for example he can enter his own email address and target's email address. what happens is that once user enter these fields, he clicks on the Button1 which is "Execute" button. this will then run the batch file to target another XML file which will generate email to the target along with other suitable fields entered in the webform. therefore, all the output can be entirely on the server side, there is no requirement for the client to view anything. i will clarify if you still have doubts – jeremychan Feb 10 '11 at 03:03
  • @jeremy: Then yes, forget about the `PAUSE` command and just run the batch file as described. No need to execute CMD.EXE. – Gabriel Magana Feb 10 '11 at 03:05
  • @gmagana thanks for the help. just discussed with my partner, he told me the reason why we run cmd.exe then antc.bat is because we need to see the output. this brings a question. are we able to print out lines output from the command prompt via a popup in the client browser? – jeremychan Feb 10 '11 at 03:16
  • @jeremy: You can capture the output, that's not a problem. See my updated answer above (Edit2, I'll call it) – Gabriel Magana Feb 10 '11 at 03:23
  • @gmagana thank you so much for your help. just one thing. i get this The Process object must have the UseShellExecute property set to false in order to redirect IO streams. from the line proc.Start() anyway for this line: proc.StartInfo.Arguments = "-parameter -parameter -etc"; what arguments do i need in this case? – jeremychan Feb 10 '11 at 05:29
  • @jeremy: It depends, if you have cmd.exe as the command, then the name of the batch file goes there. If you have the name of the batch file in your command, then the argument list is empty, unless your batch file requires parameters. – Gabriel Magana Feb 10 '11 at 05:52
  • @gmagana thanks for your help once again. do u know how to fix the error "The Process object must have the UseShellExecute property set to false in order to redirect IO streams"? this occurs at line proc.Start(); – jeremychan Feb 10 '11 at 06:24
  • @gmagana oh nevermind. i solved it. forgot to set the useshellexecute to false – jeremychan Feb 10 '11 at 06:59
1

this should be as simple as settings process1.StartInfo.Arguments = "antc"; (assuming your include path lines up or the file is in your web's working directory (and that IIS has permission to run Process())

Community
  • 1
  • 1
Brad Christie
  • 100,477
  • 16
  • 156
  • 200