Export (0) Print
Expand All

Environment.Exit Method

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public static void Exit(
	int exitCode
)

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 code example shows the use of the Exit method to terminate program execution and return an exit code to the operating system.

// 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.
*/

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

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

Community Additions

ADD
Show:
© 2014 Microsoft