Thursday, February 10, 2011

How to return ErrorCode from C# console application?

Today i had to handle one scenario in a console application.

The console application is doing some process. This console application will be called by another scheduler application. So everytime the scheduler needs to know the status of console application.
If the Console application's process is success then the console's exitcode should be '0', other wise appropriate error code should be returned.

So from C# application you can return the exit code using

System.Environment.ExitCode = ERROR CODE

To Read exitcode from calling application

Dim myProcess As New Process()             
' Start a new instance of this program but specify the 'spawned' version.             
Dim myProcessStartInfo As New ProcessStartInfo(args(0), "spawn")            
 myProcessStartInfo.UseShellExecute = False            
 myProcessStartInfo.RedirectStandardOutput = True             
myProcess.StartInfo = myProcessStartInfo             
myProcess.Start()             
Dim myStreamReader As StreamReader = myProcess.StandardOutput             
' Read the standard output of the spawned process.             
Dim myString As String = myStreamReader.ReadLine()             .
Console.WriteLine(myString)              
myProcess.WaitForExit()            
 myProcess.Close()

No comments:

Post a Comment