System.Environment Class

Assembly: Mscorlib.dll
Namespace: System
Summary
Provides information about, and means to manipulate, the current environment and platform. This class cannot be inherited.
C# Syntax:
public sealed class Environment
Remarks
Use this class to retrieve the following information:
See also:
System Namespace

System.Environment Member List:

Public Properties
CommandLine Read-only

Gets the command line for this process.
CurrentDirectory Read-write

Gets and sets the fully qualified path of the current directory; that is, the directory from which this process starts.
ExitCode Read-write

Gets or sets the exit code of the process.
HasShutdownStarted Read-only

Indicates whether the common language runtime is shutting down.
NewLine Read-only

Gets the newline string defined for this environment.
StackTrace Read-only

Gets current stack trace information.
SystemDirectory Read-only

Gets the fully qualified path of the system directory.
TickCount Read-only

Gets the number of milliseconds elapsed since the system started.
Version Read-only

Gets a Version object that describes the major, minor, build, and revision numbers of the common language runtime.
WorkingSet Read-only

Gets the amount of physical memory mapped to the process context.
Public Methods
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
Exit Terminates this process and gives the underlying operating system the specified exit code.
GetCommandLineArgs Returns a string array containing the command line arguments for the current process.
GetEnvironmentVariable Returns the value of the specified environment variable.
GetEnvironmentVariables Returns all environment variables and their values.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetLogicalDrives Returns an array of string containing the names of the logical drives on the current computer.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Environment Member Details

Property: CommandLine (read-only)
Summary
Gets the command line for this process.
C# Syntax:
public static string CommandLine {get;}
Remarks
This property provides access to the program name and any arguments specified on the command line when the current process was started.

The program name can, but is not required to, include path information. Use the Environment.GetCommandLineArgs method to retrieve the command line information parsed and stored in an array of strings.

.NET Framework Security:
EnvironmentPermission You must have EnvironmentPermissionAccess.Read permission to access the "Path" environment variable.
See also:
Environment.GetCommandLineArgs

Return to top


Property: CurrentDirectory (read-write)
Summary
Gets and sets the fully qualified path of the current directory; that is, the directory from which this process starts.
C# Syntax:
public static string CurrentDirectory {get; set;}
Exceptions
Exception Type Condition
ArgumentException Attempted to set to an empty string ("").
ArgumentNullException Attempted to set to null.
IOException An I/O error occurred.
DirectoryNotFoundException Attempted to set a path that cannot be found.
SecurityException The caller does not have the appropriate permission.
Remarks
By definition, if this process starts in the root directory of a local or network drive, the value of this property is the drive name followed by a trailing slash (for example, "C:\"). If this process starts in a subdirectory, the value of this property is the drive and subdirectory path, without a trailing slash (for example, "C:\mySubDirectory").
.NET Framework Security:
EnvironmentPermission You must have PermissionState.Unrestricted permission to get this property.
.NET Framework Security:
SecurityPermission You must have SecurityPermissionFlag.UnmanagedCode permission to set this property.

Return to top


Property: ExitCode (read-write)
Summary
Gets or sets the exit code of the process.
C# Syntax:
public static int ExitCode {get; set;}
Remarks
This property can be used to return a success code from an application. For example, it can be used to control the execution of a set of applications invoked in a script. If the value of this property is not set by an application, zero is returned.

The system exit code is set to the return value of the function, main, or zero if main returns void, regardless of whether the user sets this property.

Return to top


Property: HasShutdownStarted (read-only)
Summary
Indicates whether the common language runtime is shutting down.
C# Syntax:
public bool HasShutdownStarted {get;}
Remarks
The finalization method for an object provides you an opportunity to perform any necessary cleanup operations before the object is garbage collected. After finalization, the object is accessible but in an invalid state and therefore unusable. Eventually, garbage collection completes and reclaims the object.

An object's finalization method is called in one of the following situations: during garbage collection, when the application domain that contains the object is unloaded, or when the common language runtime is shutting down.

While executing in your finalization method when the common language runtime is shutting down, you might want to access another object that is referenced by a static field and has a finalization method. However, you cannot reliably do so because the accessed object might already have been finalized.



Note An exception to this rule is the Console class, which contains static fields that reference stream objects, but is implemented specially so you can always write to the system console, even during domain unloading or system shutdown.

Use this method in an object's finalization method to determine whether the common language runtime is shutting down. If that is the case, you cannot reliably access any object that has a finalization method and is referenced by a static field.

See also:
AppDomain.IsFinalizingForUnload

Return to top


Property: NewLine (read-only)
Summary
Gets the newline string defined for this environment.
C# Syntax:
public static string NewLine {get;}
Remarks
The property value is a constant customized specifically for the current platform.

This value is automatically appended to text when using WriteLine methods, such as Console.WriteLine.

Return to top


Property: StackTrace (read-only)
Summary
Gets current stack trace information.
C# Syntax:
public static string StackTrace {get;}
Exceptions
Exception Type Condition
ArgumentOutOfRangeException The requested stack trace information is out of range.
Remarks
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(MethodParams) in FileName: line LineNumber FullClassName, MethodName, MethodParms, FileName, and LineNumber are defined as follows: FullClassName The full name of the class, including the namespace.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 literal "at" is preceded by a single space.

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).

StackTrace might not report as many method calls as expected, due to code transformations that occur during optimization.

.NET Framework Security:
EnvironmentPermission You must have PermissionState.Unrestricted permission to access resources.

Return to top


Property: SystemDirectory (read-only)
Summary
Gets the fully qualified path of the system directory.
C# Syntax:
public static string SystemDirectory {get;}
Remarks
An example of the value returned is the string "C:\WinNT\System32".
.NET Framework Security:
FileIOPermissionAccess You must have FileIOPermissionAccess.Read permission to get this property.

Return to top


Property: TickCount (read-only)
Summary
Gets the number of milliseconds elapsed since the system started.
C# Syntax:
public static int TickCount {get;}
Remarks
The value of this property is derived from the system timer and is stored as a 32-bit signed integer. Therefore, the elapsed time will wrap around to zero if the system is run continuously for 24.9 days.

The resolution of the TickCount property cannot be less than 500 milliseconds.

The TickCount property handles an overflow condition by resetting its value to zero. The minimum value returned by TickCount is 0.

TickCount is different from the DateTime.Ticks property, which is the number of 100-nanosecond intervals that have elapsed since 1/1/0001, 12:00am.

Use the DateTime.Now property to obtain the current local date and time on this computer.

Return to top


Property: Version (read-only)
Summary
Gets a Version object that describes the major, minor, build, and revision numbers of the common language runtime.
C# Syntax:
public static Version Version {get;}

Return to top


Property: WorkingSet (read-only)
Summary
Gets the amount of physical memory mapped to the process context.
C# Syntax:
public static long WorkingSet {get;}
.NET Framework Security:
EnvironmentPermission You must have PermissionState.Unrestricted permission to access resources.

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Exit(
   int exitCode
)
Summary
Terminates this process and gives the underlying operating system the specified exit code.
C# Syntax:
public static void Exit(
   int exitCode
);
Parameters:

exitCode

Exit code to be given to the operating system.

Exceptions
Exception Type Condition
SecurityException The caller does not have sufficient security permissions to perform this function. The caller must have an unrestricted PermissionSet.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~Environment();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetCommandLineArgs()
Summary
Returns a string array containing the command line arguments for the current process.
C# Syntax:
public static string[] GetCommandLineArgs();
Return Value:
An array of string where each element contains a command line argument. The first element is the executable file name, and the following zero or more elements contain the remaining command line arguments.
Exceptions
Exception Type Condition
NotSupportedException The system does not support command line arguments.
Remarks
The first element in the array contains the file name of the executing program. If the file name is not available, the first element is equal to String.Empty. The remaining elements contain any additional tokens entered on the command line.

The program file name can, but is not required to, include path information.

To obtain the command line as a single string, use the Environment.CommandLine property.

.NET Framework Security:
EnvironmentPermission You must have EnvironmentPermissionAccess.Read permission to access the "Path" environment variable.
See also:
Environment.CommandLine

Return to top


Method: GetEnvironmentVariable(
   string variable
)
Summary
Returns the value of the specified environment variable.
C# Syntax:
public static string GetEnvironmentVariable(
   string variable
);
Parameters:

variable

A string containing the name of an environment variable.

Return Value:
A string containing the value of variable, or null if variable is not found.
Exceptions
Exception Type Condition
ArgumentNullException variable is null.
SecurityException The caller does not have EnvironmentPermission with EnvironmentPermissionAccess.Read access.
Remarks
Environment variable names are case-insensitive.
.NET Framework Security:
EnvironmentPermission You must have EnvironmentPermissionAccess.Read permission to access the "Path" environment variable.
See also:
Environment.GetEnvironmentVariables

Return to top


Method: GetEnvironmentVariables()
Summary
Returns all environment variables and their values.
C# Syntax:
public static IDictionary GetEnvironmentVariables();
Return Value:
An IDictionary, containing all environment variables and their values, or null if the system does not support environment variables.
Remarks
The names and settings for the environment variables are stored in the returned IDictionary as keys and values, respectively.
.NET Framework Security:
EnvironmentPermission You must have PermissionState.Unrestricted permission to access resources.
See also:
Environment.GetEnvironmentVariable

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetLogicalDrives()
Summary
Returns an array of string containing the names of the logical drives on the current computer.
C# Syntax:
public static string[] GetLogicalDrives();
Return Value:
An array of string where each element contains the name of a logical drive. For example, if the computer's hard drive is the first logical drive, the first element returned is "C:\".
Exceptions
Exception Type Condition
IOException An I/O error occurs.
SecurityException The caller does not have the required permissions.
.NET Framework Security:
EnvironmentPermission You must have PermissionState.Unrestricted permission to access resources.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.