Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 4
System
Environment Class
Environment Methods
 Exit Method
Collapse All/Expand All Collapse All
.NET Framework Class Library
Environment..::.Exit Method

Terminates this process and gives the underlying operating system the specified exit code.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic
<SecurityPermissionAttribute(SecurityAction.Demand, Flags := SecurityPermissionFlag.UnmanagedCode)> _
Public Shared Sub Exit ( _
    exitCode As Integer _
)
C#
[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public static void Exit(
    int exitCode
)
Visual C++
[SecurityPermissionAttribute(SecurityAction::Demand, Flags = SecurityPermissionFlag::UnmanagedCode)]
public:
static void Exit(
    int exitCode
)
F#
[<SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)>]
static member Exit : 
        exitCode:int -> unit 

Parameters

exitCode
Type: System..::.Int32
Exit code to be given to the operating system.
ExceptionCondition
SecurityException

The caller does not have sufficient security permission to perform this function.

The following example shows the use of the Exit method to terminate program execution and return an exit code to the operating system.

Visual Basic
' Example for the Environment.Exit( Integer ) method.
Imports System

Module ExitTest

    Sub Main()
        Console.WriteLine( _
            "If this program is invoked with [{0}] " & _
            "from the command prompt,", _
            Environment.CommandLine)

        Dim args As String() = Environment.GetCommandLineArgs()

        ' args[0] is the program name, and args[1] is the first argument.
        ' Test for a command-line argument.
        If args.Length > 1 Then

            ' Parse the argument. If successful, exit with the parsed code.
            Try
                Dim exitCode As Integer = Integer.Parse(args(1))

                Console.WriteLine("it exits with code: 0x{0:X8}.", exitCode)
                Environment.Exit(exitCode)

            ' If the parse fails, you fall out of the program.
            Catch 
            End Try
        End If
        Console.WriteLine("it exits by falling through.")
    End Sub 'Main
End Module 'ExitTest

' If this program is invoked with [EnvExit -2147480000] from the command prompt,
' it exits with code: 0x80000E40.
C#
// Example for the Environment.Exit( int ) method.
using System;

class ExitTest
{
    public static void Main( ) 
    {
        Console.WriteLine( 
            "If this program is invoked with [{0}] " +
            "from the command prompt,", 
            Environment.CommandLine );

        String[ ]   args = Environment.GetCommandLineArgs( );

        // args[0] is the program name and, args[1] is the first argument.
        // Test for a command-line argument.
        if( args.Length > 1 )
        {

            // Parse the argument. If successful, exit with the parsed code.
            try
            {
                int     exitCode = int.Parse( args[1] );

                Console.WriteLine( "it exits with code: 0x{0:X8}.", exitCode );
                Environment.Exit( exitCode );
            }
            // If the parse fails, you fall out of the program.
            catch
            { }
        }
        Console.WriteLine( "it exits by falling through." );
    }
}

/*
If this program is invoked with [EnvExit -2147480000] from the command prompt,
it exits with code: 0x80000E40.
*/
Visual C++
// Example for the Environment::Exit( int ) method.
using namespace System;
int main()
{
   Console::WriteLine( "If this program is invoked with [{0}] "
   "from the command prompt,", Environment::CommandLine );
   array<String^>^args = Environment::GetCommandLineArgs();

   // args[0] is the program name, and args[1] is the first argument.
   // Test for a command-line argument.
   if ( args->Length > 1 )
   {

      // Parse the argument. If successful, exit with the parsed code.
      try
      {
         int exitCode = Int32::Parse( args[ 1 ] );
         Console::WriteLine( "it exits with code: 0x{0:X8}.", exitCode );
         Environment::Exit( exitCode );
      }
      // If the parse fails, you fall out of the program.
      catch ( Exception^ e ) 
      {
      }

   }

   Console::WriteLine( "it exits by falling through." );
}

/*
If this program is invoked with [EnvExit -2147480000] from the command prompt,
it exits with code: 0x80000E40.
*/

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
List of Exit Codes?      tkstock   |   Edit   |   Show History
It might be nice to have a list of exit codes to show here or at least provide a link to them. $0$0 $0 $0I came here looking to see what the different exit codes do...$0
Tags What's this?: Add a tag
Flag as ContentBug
Dispose(), finalizers etc..      Hetzi   |   Edit   |   Show History
There ought to be a section on what does and does not happen after Environment.Exit(...) gets called.
  • using (DisposableObject X=new ...) {Environment.Exit(0);}
    Does X.Dispose get called?
  • ObjectWithFinalizer F=new ...; Environment.Exit(0);
    Does X's Finalizer get called? What about critical finalizers?
  • catch in case Environment.Exit throwing?
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker