开发者

help in running a command line in a web form

开发者 https://www.devze.com 2023-02-10 03:35 出处:网络
i would like to do an automated test system which will allow me to run a batch file automatically. right now the procedure is:

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.Pr开发者_运维技巧ocess process1 = new System.Diagnostics.Process();
    process1.StartInfo.WorkingDirectory = Request.MapPath("~/");
    process1.StartInfo.FileName = Request.MapPath("CMD.EXE"); 
    process1.Start(); 
}

thanks and regards


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())


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消