Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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)

public static class Environment

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("-- 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",

    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,

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

    Console.WriteLine("GetFolderPath: {0}", 

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


-- Environment members --
CommandLine: env0 ARBITRARY TEXT
GetCommandLineArgs: env0, ARBITRARY, TEXT
CurrentDirectory: C:\Documents and Settings\!---OMITTED---!
ExitCode: 0
HasShutdownStarted: False
MachineName: !---OMITTED---!
  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
  My system drive is C: and my system root is C:\WINNT
  My temporary directory is C:\DOCUME~1\!---OMITTED---!\LOCALS~1\Temp.
GetFolderPath: C:\WINNT\System32
GetLogicalDrives: A:\, C:\, D:\


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

© 2017 Microsoft