Understanding Environment Variables in C#

This article explains the environment class and its members and how to use them in your applications.

Consider a Real World situation in which you need to log username, machine name and so on into a log file. I have encountered this scenario many times. Usually finding such information is a very tedious job and may require some complicated code to be written.

For such types of information, Microsoft .NET provides the System.Environment class that provides some static member properties and methods to address most of such scenarios (which turns out to be a complicated implementation otherwise).

In this article, I will talk about the System.Environment class and its members and how to use them in your applications.

The Environment class defined in the System namespace allows developers to get the information about the current environment and platform.

CurrentDirectory Returns and sets the fully qualified path of the current directory; that is, the directory from which this process starts.
MachineName Returns the NetBIOS name of this local computer.
NewLine Returns the newline string defined for this environment.
OSVersion Returns an OperatingSystem object that contains the current platform identifier and version number.
SystemDirectory Returns the fully qualified path of the system directory.
UserDomainName Returns the network domain name associated with the current user.
UserName Returns the user name of the person who started the current thread.
Version Returns a Version object that describes the major, minor, build, and revision numbers of the common language runtime.
WorkingSet Returns the amount of physical memory mapped to the process context.

The Environment class also provides some useful methods.

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.
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.
GetFolderPath Gets the path to the system special folder identified by the specified enumeration.
GetLogicalDrives Returns an array of string containing the names of the logical drives on the current computer.

The following code displays the machine information and logical drives of a machine.

  1. using System;   
  2. class EnvironmentClass  
  3. {  
  4.       static void Main(string[] args)  
  5.       {  
  6.             //Getting machine and user information  
  7.             Console.WriteLine("Machine Information");  
  8.             Console.WriteLine("======================");  
  9.             Console.WriteLine("Machine Name: "+ Environment.MachineName);  
  10.             Console.WriteLine("OS Version: "+ Environment.OSVersion);  
  11.             Console.WriteLine("System Directory: "+ Environment.SystemDirectory);  
  12.             Console.WriteLine("User Name: "+ Environment.UserName);  
  13.             Console.WriteLine("Version: "+ Environment.Version);  
  14.             Console.WriteLine("Current Directory: "+ Environment.CurrentDirectory);  
  15.             Console.WriteLine();             
  16.             // Get all logical hard drives             
  17.             string[] drives = Environment.GetLogicalDrives();  
  18.             Console.WriteLine("======================");  
  19.             Console.WriteLine("Available drives:");  
  20.             foreach(string drive in drives)  
  21.                   Console.WriteLine(drive);  
  22.             Console.ReadLine();  
  23.       }  

Environment Variable