public sealed class Environment
Object
Environment
mscorlib
BCL
Provides the current settings for, and information about, the execution environment.
[Note: Use this class to retrieve the following information:
]
- Command line arguments
- Exit codes
- Environment variable settings
- Contents of the call stack
- Time since last system boot
- Version of the execution engine
System Namespace
Environment Methods
Environment.Exit Method
Environment.GetCommandLineArgs Method
Environment.GetEnvironmentVariable Method
Environment.GetEnvironmentVariables Method
Environment Properties
Environment.CommandLine Property
Environment.ExitCode Property
Environment.HasShutdownStarted Property
Environment.NewLine Property
Environment.StackTrace Property
Environment.TickCount Property
Environment.Version Property
public static void Exit(int exitCode);
Terminates the current process and sets the process exit code to the specified value.
- exitCode
- A Int32 value that is provided to the operating system.
Exception Type Condition SecurityException The immediate caller does not have the required permission.
This method causes an executing program to halt.
System.Environment Class, System Namespace
public static string[] GetCommandLineArgs();
Returns the arguments specified on the command line.
Returns a String array. Each String in the array contains a single command line argument.
The first element in the array contains the filename of the executing program. If the filename is not available, the first element is equal to System.String.Empty. The remaining elements contain any additional tokens entered on the command line.[Note: The program filename may, but is not required to, include path information.
To obtain the command line as a single String, use the System.Environment.CommandLine property.
]
System.Environment Class, System Namespace
public static string GetEnvironmentVariable(string variable);
Returns the value of the specified environment variable.
- variable
- A String containing the name of an environment variable.
A String containing the current setting of variable , ornull
.
Exception Type Condition ArgumentNullException variable is a null reference. SecurityException The caller does not have the required permission.
If variable contains a valid name for an environment variable, and if the caller has sufficient permissions, this method returns the current setting for variable. Environment variable names are case-insensitive.If variable specifies an invalid name or the system does not support environment variables, this method returns
null
.[Note: To obtain names and settings for all environment variables, use the System.Environment.GetEnvironmentVariables method.]
System.Environment Class, System Namespace
public static IDictionary GetEnvironmentVariables();
Returns all environment variables and their current settings.
A IDictionary object containing environment variable names and settings, ornull
if the system does not support environment variables.
Exception Type Condition SecurityException The caller does not have the required permission.
The names and settings for the environment variables are stored in the returned IDictionary object as keys and values, respectively.[Note: To obtain the setting of a single environment variable, use the System.Environment.GetEnvironmentVariable(System.String) method. ]
The following example prints the names and values of all environment variables defined in the environment.
using System; using System.Collections; class EnvTest:Object { public static void Main() { Console.WriteLine("Environment Variables"); IDictionary envars = Environment.GetEnvironmentVariables(); IDictionaryEnumerator varEnumerator = envars.GetEnumerator(); while(varEnumerator.MoveNext() != false) { Console.WriteLine("{0}={1}", varEnumerator.Key, varEnumerator.Value); } } }The output will vary depending on your system.
System.Environment Class, System Namespace
public static string CommandLine { get; }
Gets the information entered on the command line when the current process was started.
A String containing the command line arguments.
This property is read-only.This property provides access to the program name and any arguments specified on the command line when the current process was started.
If the environment does not support a program name, as may be the case with compact devices, then the program name is equal to System.String.Empty.
The format of the information returned by this property is implementation-defined.
[Note: The program name may, but is not required to, include path information.
Use the System.Environment.GetCommandLineArgs method to retrieve the command line information parsed and stored in an array of strings.
]
System.Environment Class, System Namespace
public static int ExitCode { get; set; }
Gets or sets the exit code of a process.
A Int32 value returned by a process. The default value is zero.
When a process exits, if the process does not return a value, the value of System.Environment.ExitCode is returned. If the value of this property is not set by an application, zero is returned.On operating systems that do not support process exit codes, CLI implementations are required to fully support getting and setting values for this property.
System.Environment Class, System Namespace
public static bool HasShutdownStarted { get; }
Gets a value indicating whether an application has started to shut down.
A Boolean wheretrue
indicates the shutdown process has started; otherwisefalse
.
This property is read-only.[Note: This property is for use inside the finalizer of an application. If the shutdown process has started, static members should not be accessed; they may have been cleaned up by the garbage collector. If the member has been cleaned up, any access attempt will cause an exception to be thrown.
System.Console.Out is a special case that is always available after the shutdown process has started.
]
System.Environment Class, System Namespace
public static string NewLine { get; }
Gets the newline string for the current platform.
A String containing the characters required to write a newline.
This property is read-only.[Note: This property is intended for platform-independent formatting of multi-line strings. This value is automatically appended to text when using
WriteLine
methods, such as System.Console.WriteLine .]
The following example demonstrates using the System.Environment.NewLine property. The string returned by System.Environment.NewLine is inserted between "Hello" and "World", causing a line break between the words in the output.
using System; class TestClass { public static void Main() { Console.WriteLine("Hello,{0}World", Environment.NewLine); } }The output is
Hello,
World
System.Environment Class, System Namespace
public static string StackTrace { get; }
Returns a string representation of the state of the call stack.
A String containing a description of the methods currently in the call stack. This value can be System.String.Empty .
This property is read-only.[Note: An example of how the String returned by this property might be formatted follows, where one line of information is provided for each method on the call stack:
at FullClassName.MethodName(MethodParms) in FileName:line LineNumber
FullClassName, MethodName, MethodParms, FileName, and LineNumber are defined as follows:
The literal "at" is preceded by a single space.
Item Description FullClassName The fully qualified name of the class. MethodName The name of the method. MethodParms The list of parameter type/name pairs. Each pair is separated by a comma (,). This information is omitted if MethodName takes zero parameters. FileName The name of the source file where the MethodName method is declared. This information is omitted if debug symbols are not available. LineNumber The number of the line in FileName that contains the source code from MethodName for the instruction that is on the call stack. This information is omitted if debug symbols are not available. The literals "in" and ":line" are omitted if debug symbols are not available.
The method calls are described in reverse chronological order (the most recent method call is described first).
System.Environment.StackTrace may not report as many method calls as expected, due to code transformations that occur during optimization.
]
The following example gets the System.Environment.StackTrace property from within a series of nested calls.
using System; public class TestCallStack { public void MyMethod1 () { MyMethod2(); } public void MyMethod2 () { MyMethod3(); } public void MyMethod3 () { Console.WriteLine("TestCallStack: {0}", Environment.StackTrace); } public static void Main() { TestCallStack t = new TestCallStack(); t.MyMethod1(); } }Without debug symbols the output is
TestCallStack: at System.Environment.GetStackTrace(Exception e)
With debug symbols the output isat System.Environment.GetStackTrace(Exception e)
at System.Environment.get_StackTrace()
at TestCallStack.Main()
TestCallStack: at System.Environment.GetStackTrace(Exception e)
at System.Environment.GetStackTrace(Exception e)
at System.Environment.get_StackTrace()
at TestCallStack.MyMethod3() in c:\ECMAExamples\envstack.cs:line 10
at TestCallStack.MyMethod2() in c:\ECMAExamples\envstack.cs:line 8
at TestCallStack.MyMethod1() in c:\ECMAExamples\envstack.cs:line 5
at TestCallStack.Main() in c:\ECMAExamples\envstack.cs:line 15
System.Environment Class, System Namespace
public static int TickCount { get; }
Gets the number of milliseconds elapsed since the system was started.
A Int32 value containing the amount of time in milliseconds that has passed since the last time the computer was started.
This property is read-only.The resolution of the System.Environment.TickCount property cannot be less than 500 milliseconds.
The value of this property is derived from the system timer.
The System.Environment.TickCount property handles an overflow condition by resetting its value to zero. The minimum value returned by System.Environment.TickCount is 0.
[Note: System.Environment.TickCount is measured in milliseconds, not in "ticks".
The System.Environment.TickCount reaches its maximum value after approximately 24.8 days of continuous up time.
For applications that require a finer granularity or a larger maximum time than System.Environment.TickCount supports, see System.DateTime.Now .
]
System.Environment Class, System Namespace
public static Version Version { get; }
Gets the current version of the execution engine.
A Version object that contains the major, minor, build, and revision numbers of the execution engine.
This property is read-only.
System.Environment Class, System Namespace