1

Possible Duplicate:
How do I specify the exit code of a console application in .net?

I need to have a console app that returns a value to the caller (I will call it by a PowerShell script).

How can I change the entry-point to the console app? (And would this be enough to be able to consume return values from the console app?)

Community
  • 1
  • 1
pencilCake
  • 51,323
  • 85
  • 226
  • 363
  • 1
    Presumably you mean exit code? http://stackoverflow.com/questions/155610/how-do-i-specify-the-exit-code-of-a-console-application-in-net – Alex K. Nov 02 '12 at 12:46

4 Answers4

4

Well, there are three obvious choices for returning information:

  • Through the exit code of the process. This is pretty easy, as you can just declare that the Main method returns int, and whatever you return from the method will be the exit code of the process. It's a bit unusual though, as the convention is for anything other than 0 to indicate some kind of error.
  • Through console output, consumed by the calling process
  • Through file output, e.g. the calling process specifies a filename, and your process writes to that file.

If you're consuming this from PowerShell though, why not write a Cmdlet? That should intergrate with PS rather more smoothly.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

You can change the main method to return an int.

See the sample

    static int Main()
    {
        return 1;
    }
Seminda
  • 1,745
  • 12
  • 15
1

You have to implement your Main method with an integer return value

static int Main(string[] args) {
    return 0;
}
dwonisch
  • 5,595
  • 2
  • 30
  • 43
0

You can try %ERRORLEVEL% environment variable.

Fredrick Gauss
  • 5,126
  • 1
  • 28
  • 44