To help test a larger piece of work, I needed a small application that I could pass a return code to that would then simply pass it back. This app would simulate TestExecute so that I can then write some Powershell to work with the return code locally rather than having to run the full application, the idea being that it would be light-weight and have a really short feedback loop.
In Visual Studio 2015, create a new Console Application solution, my Main method looks like this:
static void Main(string[] args) { Environment.ExitCode = Int32.Parse(args[0]); }
That’s all there is to it! It accepts a string array, converts the first element to an integer and assigns it to the ExitCode property of Environment.
I tried a few other ways to achieve the same outcome but this worked for me. Depending on your needs, you could have it do something more intelligent such as take the argument array and enumerate the numeric values and return text-based messages.
If you run the console app directly you will not see any output, you need to handle the return code. The first way I tested this was via a DOS batch file (runExitCode.bat):
@ECHO OFF cd C:\apps exitcodetest 1001 SET ERR=%ERRORLEVEL% echo %ERR% PAUSE
Save this file and then, in a DOS window, navigate to the folder and call the batch file: runExitCode.bat 10
The output, the value that you passed in, will be shown on the next line.
This is all I need the app to do – I will show in another post how I use this app with Powershell and in turn how I use that return code in NAnt.