-1

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.ExitCode is not inherited from errorlevel, %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.

cdlvcdlv
  • 952
  • 1
  • 9
  • 22
  • 1
    0 is the default, no-error indication. If you set it, your application should terminate. So... why do you want to `read` it? – Stefan Jul 16 '18 at 11:52
  • 1
    Possible duplicate of [How do I specify the exit code of a console application in .NET?](https://stackoverflow.com/questions/155610/how-do-i-specify-the-exit-code-of-a-console-application-in-net) – mjwills Jul 16 '18 at 11:54
  • It would be awesome if you could provide a [mcve]. As is, this feels like a XY Problem - https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem - since we can't see the context. – mjwills Jul 16 '18 at 12:00
  • This code just proves that, without touching it, `Environment.ExitCode` begins as 0. If I was able to _get_ it then I would _set_ it. – cdlvcdlv Jul 16 '18 at 12:01
  • 2
    Unfortunately, `ERRORLEVEL` is built into the command interpreter. It's not, for example, some form of environment variable. You can't access whatever it was left set to following the previous program's exit. – Damien_The_Unbeliever Jul 16 '18 at 12:06
  • To downvoters/dupevoters, please read my updates. – cdlvcdlv Jul 17 '18 at 09:05

1 Answers1

0

You cannot read the ERRORLEVEL from the last program, but you could pass it as argument and then set it again:

class Program
{
    static void Main(string[] args)
    {
        Environment.ExitCode = int.Parse(args[0]);
    }
}

And then call it in your batch like this:

cd "some path that returns an error"
.\Console1App.exe %errorlevel%

REM Print the error code of the cd command
echo %errorlevel%

Note that this only works if the %errorlevel% environment variable is not set. The way CMD works is that it will pass the actual errorlevel as fallback when %errorlevel% is not set

Freggar
  • 1,049
  • 1
  • 11
  • 24
  • 1
    [ERRORLEVEL is not %ERRORLEVEL%](https://blogs.msdn.microsoft.com/oldnewthing/20080926-00/?p=20743) by Raymond Chen might be instructive. – Damien_The_Unbeliever Jul 16 '18 at 12:08
  • @Freggar OK. If you say it's impossible I'll take your word but in such cases it comes to my mind that quote from _The Princess Bride_: «Nonsense. You're only saying that because no one ever has.» ᴶᵘˢᵗ ᵏᶦᵈᵈᶦⁿᵍ – cdlvcdlv Jul 16 '18 at 21:35
  • The ERRORLEVEL of the last executed command is internally saved by the interpreter (CMD) but never actually written to any enviroment variable like in UNIX systems. It's just a quirk of CMD that it subsitutes `%errorlevel%` with `ERRORLEVEL` when the enviroment variable is not set. – Freggar Jul 18 '18 at 07:11