Is there a way to read ERRORLEVEL from an C# so you can return it and keeps unchanged when finishes?
It seems that Environment.ExitCode begins with 0 when the program starts. This program always writes 0.
To too lazy; didn't read people: the following code is not the program I want to write, but a test to prove that
Environment.ExitCodeis not inherited fromerrorlevel,%errorlevel%or any previous exit code just in case of doubt.
using System;
namespace test {
    class Program {
        static void Main() {
            Console.WriteLine("{0}",Environment.ExitCode);
        }
    }
}
My goal is to write a program that, when used in a batch, doesn't interfere with the current errorlevel so it can be used afterwards. I know you can save it and restore it inside the batch...
...
command1 with options "or not"
set elsaved=%errorlevel%
my_cs_program run without telling it which errorlevel is
REM Damn, I cleared errorlevel, thank science I saved it before
call :setel %elsaved%
...
goto :eof
:setel
exit /b %1
... but it's a nuisance. It would be better if the invocation left errorlevel alone.
...
        static void Main() {
...
            var parent_cmd_errorlevel=exotic_funtion();
...
            Environment.ExitCode = parent_cmd_errorlevel;
...
This way I don't have to mind about it in the batch.
tl;dr: Is it possible to write a C# program transparent to errorlevel? If not in C#, is there any other language? What one? More generally: can a cmd.exe child process obtain the errorlevel of its parent? Maybe wmic would help?
To downvoters/duplicate-voters: I know how to specify the exit code of a console application in .NET:
...
        static void Main() {
...
            Environment.ExitCode = whatever;
...
Or
...
        static int Main() {
...
            return(whatever);
...
Am I wrong? If you are not interested in this problem, please leave the question alone but AFAIK this has not been asked in SO or another SE KB.
 
    