0

I am working on a project that controls some .cmd from a web application. Now I am stucked in a problem. I want to pass an error message from batch file to c#. That means if any error happens in .cmd, an error message will show in my web applicaton. How can I do this? I am a beginner. I really don't get any solution for this.

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123

2 Answers2

2

look at this Executing Batch File in C#

basically you absorb all the console and based on the exit code you know if there is an error and then pass that to your client

processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
// *** Redirect the output ***
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;

process = Process.Start(processInfo);
process.WaitForExit();

// *** Read the streams ***
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();

the the error can be passed to the client. The exact method by which this happens depend on your web implementation

Community
  • 1
  • 1
n00b
  • 1,832
  • 14
  • 25
  • Basically I have an error message on my batch command. When any error happens it shows it to my command prompt when I run it. When error happens a message "error" prompt in my command prompt. I want this "error" would come to my web application in a message box. I hope you understand the problem. please help me. – Md. Fazle Rabbi Jun 11 '15 at 06:41
0
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();

use the output variable for showing the standard output and error variable for showing the standard error.

Thanks a lot #Braim for your code

Community
  • 1
  • 1