Environment Class

Provides information about, and means to manipulate, the current environment and platform. This class cannot be inherited.

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

[ComVisibleAttribute(true)]
public static class Environment

The Environment type exposes the following members.

  NameDescription
Public propertyStatic memberCommandLineGets the command line for this process.
Public propertyStatic memberCurrentDirectoryGets or sets the fully qualified path of the current working directory.
Public propertyStatic memberSupported in .NET for Windows Store appsCurrentManagedThreadIdGets a unique identifier for the current managed thread.
Public propertyStatic memberExitCodeGets or sets the exit code of the process.
Public propertyStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsHasShutdownStartedGets a value that indicates whether the current application domain is being unloaded or the common language runtime (CLR) is shutting down.
Public propertyStatic memberIs64BitOperatingSystemDetermines whether the current operating system is a 64-bit operating system.
Public propertyStatic memberIs64BitProcessDetermines whether the current process is a 64-bit process.
Public propertyStatic memberMachineNameGets the NetBIOS name of this local computer.
Public propertyStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsNewLineGets the newline string defined for this environment.
Public propertyStatic memberSupported by the XNA FrameworkOSVersionGets an OperatingSystem object that contains the current platform identifier and version number.
Public propertyStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsProcessorCountGets the number of processors on the current machine.
Public propertyStatic memberStackTraceGets current stack trace information.
Public propertyStatic memberSystemDirectoryGets the fully qualified path of the system directory.
Public propertyStatic memberSystemPageSizeGets the number of bytes in the operating system's memory page.
Public propertyStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsTickCountGets the number of milliseconds elapsed since the system started.
Public propertyStatic memberUserDomainNameGets the network domain name associated with the current user.
Public propertyStatic memberUserInteractiveGets a value indicating whether the current process is running in user interactive mode.
Public propertyStatic memberUserNameGets the user name of the person who is currently logged on to the Windows operating system.
Public propertyStatic memberSupported by the XNA FrameworkVersionGets a Version object that describes the major, minor, build, and revision numbers of the common language runtime.
Public propertyStatic memberWorkingSetGets the amount of physical memory mapped to the process context.
Top

  NameDescription
Public methodStatic memberExitTerminates this process and gives the underlying operating system the specified exit code.
Public methodStatic memberExpandEnvironmentVariablesReplaces the name of each environment variable embedded in the specified string with the string equivalent of the value of the variable, then returns the resulting string.
Public methodStatic memberSupported in .NET for Windows Store appsFailFast(String)Immediately terminates a process after writing a message to the Windows Application event log, and then includes the message in error reporting to Microsoft.
Public methodStatic memberSupported in .NET for Windows Store appsFailFast(String, Exception)Immediately terminates a process after writing a message to the Windows Application event log, and then includes the message and exception information in error reporting to Microsoft.
Public methodStatic memberGetCommandLineArgsReturns a string array containing the command-line arguments for the current process.
Public methodStatic memberGetEnvironmentVariable(String)Retrieves the value of an environment variable from the current process.
Public methodStatic memberGetEnvironmentVariable(String, EnvironmentVariableTarget)Retrieves the value of an environment variable from the current process or from the Windows operating system registry key for the current user or local machine.
Public methodStatic memberGetEnvironmentVariables()Retrieves all environment variable names and their values from the current process.
Public methodStatic memberGetEnvironmentVariables(EnvironmentVariableTarget)Retrieves all environment variable names and their values from the current process, or from the Windows operating system registry key for the current user or local machine.
Public methodStatic memberSupported by the XNA FrameworkGetFolderPath(Environment.SpecialFolder)Gets the path to the system special folder that is identified by the specified enumeration.
Public methodStatic memberGetFolderPath(Environment.SpecialFolder, Environment.SpecialFolderOption)Gets the path to the system special folder that is identified by the specified enumeration, and uses a specified option for accessing special folders.
Public methodStatic memberGetLogicalDrivesReturns an array of string containing the names of the logical drives on the current computer.
Public methodStatic memberSetEnvironmentVariable(String, String)Creates, modifies, or deletes an environment variable stored in the current process.
Public methodStatic memberSetEnvironmentVariable(String, String, EnvironmentVariableTarget)Creates, modifies, or deletes an environment variable stored in the current process or in the Windows operating system registry key reserved for the current user or local machine.
Top

Use the Environment class to retrieve information such as command-line arguments, the exit code, environment variable settings, contents of the call stack, time since last system boot, and the version of the common language runtime.

The following example demonstrates displays a list of information about the current environment.

// Sample for Environment class summary 
using System;
using System.Collections;

class Sample 
{
    public static void Main() 
    {
    String str;
    String nl = Environment.NewLine;
//
    Console.WriteLine();
    Console.WriteLine("-- Environment members --");

//  Invoke this sample with an arbitrary set of command line arguments.
    Console.WriteLine("CommandLine: {0}", Environment.CommandLine);

    String[] arguments = Environment.GetCommandLineArgs();
    Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", arguments));

//  <-- Keep this information secure! -->
    Console.WriteLine("CurrentDirectory: {0}", Environment.CurrentDirectory);

    Console.WriteLine("ExitCode: {0}", Environment.ExitCode);

    Console.WriteLine("HasShutdownStarted: {0}", Environment.HasShutdownStarted);

//  <-- Keep this information secure! -->
    Console.WriteLine("MachineName: {0}", Environment.MachineName);

    Console.WriteLine("NewLine: {0}  first line{0}  second line{0}  third line",
                          Environment.NewLine);

    Console.WriteLine("OSVersion: {0}", Environment.OSVersion.ToString());

    Console.WriteLine("StackTrace: '{0}'", Environment.StackTrace);

//  <-- Keep this information secure! -->
    Console.WriteLine("SystemDirectory: {0}", Environment.SystemDirectory);

    Console.WriteLine("TickCount: {0}", Environment.TickCount);

//  <-- Keep this information secure! -->
    Console.WriteLine("UserDomainName: {0}", Environment.UserDomainName);

    Console.WriteLine("UserInteractive: {0}", Environment.UserInteractive);

//  <-- Keep this information secure! -->
    Console.WriteLine("UserName: {0}", Environment.UserName);

    Console.WriteLine("Version: {0}", Environment.Version.ToString());

    Console.WriteLine("WorkingSet: {0}", Environment.WorkingSet);

//  No example for Exit(exitCode) because doing so would terminate this example. 

//  <-- Keep this information secure! -->
    String query = "My system drive is %SystemDrive% and my system root is %SystemRoot%";
    str = Environment.ExpandEnvironmentVariables(query);
    Console.WriteLine("ExpandEnvironmentVariables: {0}  {1}", nl, str);

    Console.WriteLine("GetEnvironmentVariable: {0}  My temporary directory is {1}.", nl,
                           Environment.GetEnvironmentVariable("TEMP"));

    Console.WriteLine("GetEnvironmentVariables: ");
    IDictionary	environmentVariables = Environment.GetEnvironmentVariables();
    foreach (DictionaryEntry de in environmentVariables)
        {
        Console.WriteLine("  {0} = {1}", de.Key, de.Value);
        }

    Console.WriteLine("GetFolderPath: {0}", 
                 Environment.GetFolderPath(Environment.SpecialFolder.System));

    String[] drives = Environment.GetLogicalDrives();
    Console.WriteLine("GetLogicalDrives: {0}", String.Join(", ", drives));
    }
}
/*
This example produces results similar to the following:
(Any result that is lengthy or reveals information that should remain 
secure has been omitted and marked "!---OMITTED---!".)

C:\>env0 ARBITRARY TEXT

-- Environment members --
CommandLine: env0 ARBITRARY TEXT
GetCommandLineArgs: env0, ARBITRARY, TEXT
CurrentDirectory: C:\Documents and Settings\!---OMITTED---!
ExitCode: 0
HasShutdownStarted: False
MachineName: !---OMITTED---!
NewLine:
  first line
  second line
  third line
OSVersion: Microsoft Windows NT 5.1.2600.0
StackTrace: '   at System.Environment.GetStackTrace(Exception e)
   at System.Environment.GetStackTrace(Exception e)
   at System.Environment.get_StackTrace()
   at Sample.Main()'
SystemDirectory: C:\WINNT\System32
TickCount: 17995355
UserDomainName: !---OMITTED---!
UserInteractive: True
UserName: !---OMITTED---!
Version: !---OMITTED---!
WorkingSet: 5038080
ExpandEnvironmentVariables:
  My system drive is C: and my system root is C:\WINNT
GetEnvironmentVariable:
  My temporary directory is C:\DOCUME~1\!---OMITTED---!\LOCALS~1\Temp.
GetEnvironmentVariables: 
  !---OMITTED---!
GetFolderPath: C:\WINNT\System32
GetLogicalDrives: A:\, C:\, D:\

*/

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft