1

I have a BAT file that I am running using the following code:

@ECHO ON
java com.mypackage.test send

which result in this:

C:\myfolder>java com.mypackage.test send
EXEC Sending...
Received 1 response(s)
Status Code: C00

How do I take the Status Code value (C00) and save it to a string in my WinForm C# application so I can use it for other actions?

I set the..

proc.StartInfo.RedirectStandardError = false; proc.StartInfo.RedirectStandardOutput = true;

but not sure what to do next...

Si8
  • 9,141
  • 22
  • 109
  • 221

4 Answers4

4

This is an example of reading a directory listing. Of course it is relatively easy to change for your requirements.

void Main()
{
    StringBuilder sb = new StringBuilder();
    var pSpawn = new Process
    {
         StartInfo = 
         { 
            WorkingDirectory = @"D:\temp", 
            FileName = "cmd.exe", 
            Arguments ="/c dir /b", 
            CreateNoWindow = true,
            RedirectStandardOutput = true,
            RedirectStandardInput = true,
            UseShellExecute = false
         }
    };


    pSpawn.OutputDataReceived += (sender, args) => sb.AppendLine(args.Data);
    pSpawn.Start();
    pSpawn.BeginOutputReadLine();
    pSpawn.WaitForExit();
    Console.WriteLine(sb.ToString());
}
Steve
  • 213,761
  • 22
  • 232
  • 286
1
proc.Start();    // Start the proccess
proc.WaitForExit();
var stream = proc.StandardOutput;

the Process.StandardOutput returns a StreamReader, which you then can read line by line with the ReadLine method. You can then parse for the desired output

David
  • 10,458
  • 1
  • 28
  • 40
1

If you just want an output and don't care to code this in C#, just modify your batch file to send the output to output.log:

@ECHO ON
java com.mypackage.test send > output.log

Your application can then open and process the log file.

Hope this helps.

1
//create the process
Process p = new Process();

//redirect the output stream
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "myfile.bat";
p.Start();

//read the output stream
string statusCode = p.StandardOutput.ReadToEnd();
p.WaitForExit();
nacho
  • 26
  • 2