I have a WPF application that takes command line arguments and in some cases returns an exit value without starting the App. I have replaced the entry point as in this question Replacing the WPF entry point. When I run in Visual Studio without an argument it launches my App as expected. When I supply an argument it all looks good I get a line telling me "The program XXX has exited with code 66"
When I then run from the CMD command line without an argument it launches the app as expected. When I supply an argument and look at %ERRORLEVEL% I get an exit code of 0 not 66. What gives here ?
namespace WpfApplication2
{
public class EntryPoint
{
[STAThread]
public static void Main(string[] args)
{
if (args.Count > 0)
{
Environment.Exit(66);
}
App.Main();
}
}
public partial class App : Application
{
}
}
Your application is windowed, and runs in the background as far as the command window is concerned, so %errorlevel%
only shows that the application was launched. When your application later exits, the exit code is lost.
As described in https://stackoverflow.com/a/11476681, if you launch your application with START /WAIT
, then the command window will wait for your application to exit and store the exit code in %errorlevel%
.