3

Following is my code to run a command from aspx page using c#:

 string strcmd = "/k" + @"cd: C:\GWATS\bin\&&" + "waft_exe";
 System.Diagnostics.Process.Start("cmd.exe", strcmd); 
 Response.Redirect("./result.aspx");

This is working fine. But I want to get the output from the cmd to my aspx page. Can anyone give me coding for this? I am working on ASP.NET just for the past 1 week, hence I dont have any idea of this. So please give me a working code for this?

rajasekar25
  • 309
  • 1
  • 3
  • 19

1 Answers1

2

You can get and read the Output from your command as:

var cCommandEx = System.Diagnostics.Process.Start("cmd.exe", strcmd);

string cRetStandardOut = cCommandEx.StandardOutput.ReadToEnd();

// before the redirect wait to exit and close it
cCommandEx.WaitForExit();
cCommandEx.Close();

and use it for your redirect.

You must also have set RedirectStandardOutput = true, on the ProcessStartInfo() of your process, if you do not read anything as it is.

Aristos
  • 66,005
  • 16
  • 114
  • 150