Posted by: Cirilo Meggiolaro | 12/13/2008

Tip of the day #60 – Environment class

Let’s check today the Environment class that provides a lot of really useful properties and methods to retrieve and manipulate some settings of the environment and platform such as domain, user and machine name and environment variables.


The following static properties are available under the Environment class:

  • string CommandLine: Retrieves the command line for the current process;
  • string CurrentDirectory: Retrieves or defines the current working directory path;
  • int ExitCode: Retrieves or defines the exit code of the process;
  • bool HasShutdownStarted: Retrieves a Boolean flag indicating whether the CLR is shutting down or the current application domain is unloading;
  • string MachineName: Retrieves the name of the local computer based on the NetBIOS;
  • string NewLine: Retrieves the newline string defined for this environment (“\r\n” for non-Unix platforms or “\n” for Unix platforms);
  • OperatingSystem OSVersion: Retrieves an OperatingSystem object that contains the current platform identifier and version number;
  • int ProcessorCount: Retrieves the number of processors on the current computer;
  • string StackTrace: Gets the current stack trace information;
  • string SystemDirectory: Retrieves the system directory path;
  • int TickCount: Retrieves the number of milliseconds elapsed since the system started;
  • string UserDomainName: Retrieves the network domain name associated with the current user;
  • bool UserInteractive: Retrieves a value indicating whether the current process is running in user interactive mode;
  • string UserName: Retrieves the user name of the person who is currently logged on;
  • Version Version: Retrieves a System.Version object that describes the major, minor, build, and revision numbers of the CLR;
  • long WorkingSet: Retrieves the amount of physical memory mapped to the process context.


  • Exit: Terminates the current process and gives the underlying operating system the specified exit code;
  • ExpandEnvironmentVariables: Replaces 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;
  • FailFast: Terminates a process but does not execute any active try-finally blocks or finalizers;
  • GetCommandLineArgs: Retrieves the command-line arguments for the current process;
  • GetEnvironmentVariable (2 overloads): 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;
  • GetEnvironmentVariables (2 overloads): 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;
  • GetFolderPath: Retrieves the system special folder path identified by the specified enumeration;
  • GetLogicalDrives: Retrieves the names of the logical drives on the current computer;
  • SetEnvironmentVariable (2 overloads): 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.

How to…

The following code snippet writes to a console window all the properties available under the Environment class:

Console.WriteLine(“CommandLine: {0}”, Environment.CommandLine);
Console.WriteLine(“CurrentDirectory: {0}”, Environment.CurrentDirectory);
Console.WriteLine(“ExitCode: {0}”, Environment.ExitCode.ToString());
Console.WriteLine(“HasShutdownStarted: {0}”, Environment.HasShutdownStarted.ToString());
Console.WriteLine(“MachineName: {0}”, Environment.MachineName);
Console.WriteLine(“NewLine: {0}”, Environment.NewLine);
Console.WriteLine(“OSVersion Platform: {0}”, Environment.OSVersion.Platform.ToString());
Console.WriteLine(“OSVersion Service Pack: {0}”, Environment.OSVersion.ServicePack);
Console.WriteLine(“OSVersion Full String: {0}”, Environment.OSVersion.Version.ToString());
Console.WriteLine(“OSVersion VersionString: {0}”, Environment.OSVersion.VersionString);
Console.WriteLine(“ProcessorCount: {0}”, Environment.ProcessorCount.ToString());
Console.WriteLine(“StackTrace: {0}”, Environment.StackTrace);
Console.WriteLine(“SystemDirectory: {0}”, Environment.SystemDirectory);
Console.WriteLine(“TickCount: {0}”, Environment.TickCount.ToString());
Console.WriteLine(“UserDomainName: {0}”, Environment.UserDomainName);
Console.WriteLine(“UserInteractive: {0}”, Environment.UserInteractive.ToString());
Console.WriteLine(“UserName: {0}”, Environment.UserName);
Console.WriteLine(“CLR Version: {0}”, Environment.Version.ToString());
Console.WriteLine(“WorkingSet: {0}”, Environment.WorkingSet.ToString());

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: