Environment.ExitCode Proprietà

Definizione

Ottiene o imposta il codice di uscita del processo.

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

Valore della proprietà

Int32

Intero con segno a 32 bit contenente il codice di uscita. Il valore predefinito è 0 (zero), che indica che il processo è stato completato con successo.

Esempio

Di seguito è riportata una semplice app denominata Double.exe che doppia un valore intero passato come argomento della riga di comando. Il valore assegna codici di errore alla ExitCode proprietà per indicare le condizioni di errore. Si noti che è necessario aggiungere un riferimento all'assembly di System.Numerics.dll per compilare correttamente l'esempio.

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

L'esempio può quindi essere richiamato da un file batch, ad esempio il seguente, che rende accessibili i codici di errore usando il ERRORLEVEL comando .

@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

Di seguito viene illustrato un output di esempio prodotto richiamando il file batch.

>getdouble 123
Result: 246
Completed Successfully

>getdouble 5912323109093
Arithmetic overflow

>getdouble
Missing argument

>getdouble "a string"
Invalid argument

Si noti che il codice per Double.exe è identico in funzione all'esempio seguente, ad eccezione del fatto che l'ex definisce un punto di ingresso denominato senza valore restituito, mentre questo esempio definisce un punto di ingresso denominato Main Main che restituisce un intero.

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

Commenti

Se il Main metodo restituisce void, è possibile usare questa proprietà per impostare il codice di uscita che verrà restituito all'ambiente chiamante. Se Main non restituisce void, questa proprietà viene ignorata. Il valore iniziale di questa proprietà è zero.

Avviso

La ExitCode proprietà è un intero a 32 bit firmato. Per impedire alla proprietà di restituire un codice di uscita negativo, non è consigliabile usare valori maggiori o uguali a 0x80000000.

Usare un numero diverso da zero per indicare un errore. Nell'applicazione è possibile definire i propri codici di errore in un'enumerazione e restituire il codice di errore appropriato in base allo scenario. Ad esempio, restituisce un valore pari a 1 per indicare che il file richiesto non è presente e un valore pari a 2 per indicare che il file è in formato errato. Per un elenco di codici di uscita usati dal sistema operativo Windows, vedere Codici di errore di sistema nella documentazione di Windows.

Si applica a