Environment.ExitCode Eigenschap

Definitie

Hiermee haalt u de afsluitcode van het proces op of stelt u deze in.

public:
 static property int ExitCode { int get(); void set(int value); };
public static int ExitCode { get; set; }
static member ExitCode : int with get, set
Public Shared Property ExitCode As Integer

Waarde van eigenschap

Een 32-bits geheel getal met de afsluitcode. De standaardwaarde is 0 (nul), wat aangeeft dat het proces is voltooid.

Voorbeelden

Hier volgt een eenvoudige app met de naam Double.exe waarmee een geheel getal wordt verdubbeld dat als opdrachtregelargument wordt doorgegeven. De waarde wijst foutcodes toe aan de ExitCode eigenschap om foutvoorwaarden aan te geven. Houd er rekening mee dat u een verwijzing naar de System.Numerics.dll assembly moet toevoegen om het voorbeeld te compileren.

using System;
using System.Numerics;

public class Example
{
   private const int ERROR_BAD_ARGUMENTS = 0xA0;
   private const int ERROR_ARITHMETIC_OVERFLOW = 0x216;
   private const int ERROR_INVALID_COMMAND_LINE = 0x667;

   public static void Main()
   {
      string[] args = Environment.GetCommandLineArgs();
      if (args.Length == 1) {
         Environment.ExitCode = ERROR_INVALID_COMMAND_LINE;
      }
      else {
         BigInteger value = 0;
         if (BigInteger.TryParse(args[1], out value))
            if (value <= Int32.MinValue || value >= Int32.MaxValue)
               Environment.ExitCode = ERROR_ARITHMETIC_OVERFLOW;
            else
               Console.WriteLine("Result: {0}", value * 2);

         else
            Environment.ExitCode = ERROR_BAD_ARGUMENTS;
      }
   }
}
open System
open System.Numerics

let ERROR_BAD_ARGUMENTS = 0xA0
let ERROR_ARITHMETIC_OVERFLOW = 0x216
let ERROR_INVALID_COMMAND_LINE = 0x667

let args = Environment.GetCommandLineArgs()
if args.Length = 1 then
    Environment.ExitCode <- ERROR_INVALID_COMMAND_LINE
else
    match BigInteger.TryParse args[1] with
    | true, value ->
        if value <= bigint Int32.MinValue || value >= bigint Int32.MaxValue then
            Environment.ExitCode <- ERROR_ARITHMETIC_OVERFLOW
        else
            printfn $"Result: {value * 2I}"
    | _ ->
        Environment.ExitCode <- ERROR_BAD_ARGUMENTS
Imports System.Numerics

Module Example
   Private Const ERROR_BAD_ARGUMENTS As Integer = &hA0
   Private Const ERROR_ARITHMETIC_OVERFLOW As Integer = &h216
   Private Const ERROR_INVALID_COMMAND_LINE As Integer = &h667
    
   Public Sub Main()
      Dim args() As String = Environment.GetCommandLineArgs()
      If args.Length = 1 Then
         Environment.ExitCode = ERROR_INVALID_COMMAND_LINE  
      Else
         Dim value As BigInteger = 0
         If BigInteger.TryParse(args(1), value) Then
            If value <= Int32.MinValue Or value >= Int32.MaxValue
               Environment.ExitCode = ERROR_ARITHMETIC_OVERFLOW
            Else
               Console.WriteLine("Result: {0}", value * 2)
            End If
         Else
            Environment.ExitCode = ERROR_BAD_ARGUMENTS
         End If     
      End If
   End Sub
End Module

Het voorbeeld kan vervolgens worden aangeroepen vanuit een batchbestand, zoals het volgende, waardoor de bijbehorende foutcodes toegankelijk zijn met behulp van de ERRORLEVEL opdracht.

@echo off
Double.exe %1
if errorlevel 1639 goto NoArg
if errorlevel 534 goto Overflow
if errorlevel 160 goto BadArg
if errorlevel 0 echo Completed Successfully
goto :EOF

:NoArg
echo Missing argument
goto :EOF

:Overflow
echo Arithmetic overflow
goto :EOF

:BadArg
echo Invalid argument
goto :EOF

Hieronder ziet u enkele voorbeelduitvoer die wordt geproduceerd door het batchbestand aan te roepen.

>getdouble 123
Result: 246
Completed Successfully

>getdouble 5912323109093
Arithmetic overflow

>getdouble
Missing argument

>getdouble "a string"
Invalid argument

Houd er rekening mee dat de code voor Double.exe identiek is aan het volgende voorbeeld, behalve dat de eerste een toegangspunt Main definieert dat geen retourwaarde heeft, terwijl in dit voorbeeld een toegangspunt Main wordt gedefinieerd dat een geheel getal retourneert.

using System;
using System.Numerics;

public class Example
{
   private const int ERROR_SUCCESS = 0;
   private const int ERROR_BAD_ARGUMENTS = 0xA0;
   private const int ERROR_ARITHMETIC_OVERFLOW = 0x216;
   private const int ERROR_INVALID_COMMAND_LINE = 0x667;

   public static int Main()
   {
      string[] args = Environment.GetCommandLineArgs();
      if (args.Length == 1) {
         return ERROR_INVALID_COMMAND_LINE;
      }
      else {
         BigInteger value = 0;
         if (BigInteger.TryParse(args[1], out value))
            if (value <= Int32.MinValue || value >= Int32.MaxValue)
               return ERROR_ARITHMETIC_OVERFLOW;
            else
               Console.WriteLine("Result: {0}", value * 2);

         else
            return ERROR_BAD_ARGUMENTS;
      }
      return ERROR_SUCCESS;
   }
}
open System
open System.Numerics

let ERROR_SUCCESS = 0
let ERROR_BAD_ARGUMENTS = 0xA0
let ERROR_ARITHMETIC_OVERFLOW = 0x216
let ERROR_INVALID_COMMAND_LINE = 0x667

[<EntryPoint>]
let main _ =
    let args = Environment.GetCommandLineArgs()
    if args.Length = 1 then
        ERROR_INVALID_COMMAND_LINE
    else
        match BigInteger.TryParse args[1] with
        | true, value ->
            if value <= bigint Int32.MinValue || value >= bigint Int32.MaxValue then
                ERROR_ARITHMETIC_OVERFLOW
            else
                printfn $"Result: {value * 2I}"
                ERROR_SUCCESS
        | _ ->
            ERROR_BAD_ARGUMENTS
Imports System.Numerics

Module Example
   Private Const ERROR_SUCCESS As Integer = 0
   Private Const ERROR_BAD_ARGUMENTS As Integer = &hA0
   Private Const ERROR_ARITHMETIC_OVERFLOW As Integer = &h216
   Private Const ERROR_INVALID_COMMAND_LINE As Integer = &h667
    
   Public Function Main() As Integer
      Dim args() As String = Environment.GetCommandLineArgs()
      If args.Length = 1 Then
         Return ERROR_INVALID_COMMAND_LINE  
      Else
         Dim value As BigInteger = 0
         If BigInteger.TryParse(args(1), value) Then
            If value <= Int32.MinValue Or value >= Int32.MaxValue
               Return ERROR_ARITHMETIC_OVERFLOW
            Else
               Console.WriteLine("Result: {0}", value * 2)
            End If
         Else
            Return ERROR_BAD_ARGUMENTS
         End If     
      End If
      Return ERROR_SUCCESS
   End Function
End Module

Opmerkingen

Als de Main methode retourneert void, kunt u deze eigenschap gebruiken om de afsluitcode in te stellen die wordt geretourneerd naar de aanroepende omgeving. Als Main deze eigenschap niet wordt geretourneerd void, wordt deze eigenschap genegeerd. De oorspronkelijke waarde van deze eigenschap is nul.

Waarschuwing

De ExitCode eigenschap is een ondertekend 32-bits geheel getal. Als u wilt voorkomen dat de eigenschap een negatieve afsluitcode retourneert, moet u geen waarden gebruiken die groter zijn dan of gelijk zijn aan 0x80000000.

Gebruik een niet-nul getal om een fout aan te geven. In uw toepassing kunt u uw eigen foutcodes definiƫren in een opsomming en de juiste foutcode retourneren op basis van het scenario. Retourneer bijvoorbeeld een waarde van 1 om aan te geven dat het vereiste bestand niet aanwezig is en een waarde van 2 om aan te geven dat het bestand de verkeerde indeling heeft. Zie System Error Codes in de Windows documentatie voor een lijst met afsluitcodes die door het Windows besturingssysteem worden gebruikt.

Van toepassing op