Environment.Exit 方法

终止此进程并为基础操作系统提供指定的退出代码。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Shared Sub Exit ( _
    exitCode As Integer _
)
用法
Dim exitCode As Integer

Environment.Exit(exitCode)
public static void Exit (
    int exitCode
)
public:
static void Exit (
    int exitCode
)
public static void Exit (
    int exitCode
)
public static function Exit (
    exitCode : int
)

参数

  • exitCode
    提供给操作系统的退出代码。

异常

异常类型 条件

SecurityException

调用方没有足够的安全权限来执行此函数。

示例

下面的代码示例说明如何使用 Exit 方法终止程序执行并向操作系统返回退出代码。

' 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.
// 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.
*/
// 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.
*/
// Example for the Environment.Exit(int) method.
import System.* ;

class ExitTest
{
    public static void main(String[] args)
    {
        Console.WriteLine("If this program is invoked with [{0}] " +
            "from the command prompt,", Environment.get_CommandLine());
        String args1[] = Environment.GetCommandLineArgs();

        // args[0] is the program name and, args[1] is the first argument.
        // Test for a command-line argument.
        if (args1.length > 1) {
            // Parse the argument. If successful, exit with the parsed code.
            try {
                int exitCode = Integer.parseInt(args1[1]);
                Console.WriteLine("it exits with code: 0x{0:X8}.",
                    ((System.Int32)(exitCode)).ToString("X8") );
                Environment.Exit(exitCode);
            }
            // If the parse fails, you fall out of the program.
            catch(System.Exception exp) {
            }
        }
        Console.WriteLine("it exits by falling through.");
    } //main
} //ExitTest

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

.NET Framework 安全性

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

Environment 类
Environment 成员
System 命名空间