How do I redirect command line output to an ASPX page using C #?

Below 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 works great. But I want to get output from cmd to my aspx page. Can anyone give me an encoding for this? I've only been working on ASP.NET for the past week, so I don't know about it. So please give me a working code for this?

+3


source to share


1 answer


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

You should also install RedirectStandardOutput = true,

in ProcessStartInfo()

your process if you don't read anything as it is.

+2


source







All Articles