Understanding Environment Variables in C#

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.

using System;   
class EnvironmentClass  
{  
    static void Main(string[] args)  
    {  
        //Getting machine and user information  
        Console.WriteLine("Machine Information");  
        Console.WriteLine("======================");  
        Console.WriteLine("Machine Name: "+ Environment.MachineName);  
        Console.WriteLine("OS Version: "+ Environment.OSVersion);  
        Console.WriteLine("System Directory: "+ Environment.SystemDirectory);  
        Console.WriteLine("User Name: "+ Environment.UserName);  
        Console.WriteLine("Version: "+ Environment.Version);  
        Console.WriteLine("Current Directory: "+ Environment.CurrentDirectory);  
        Console.WriteLine();             
        // Get all logical hard drives             
        string[] drives = Environment.GetLogicalDrives();  
        Console.WriteLine("======================");  
        Console.WriteLine("Available drives:");  
        foreach(string drive in drives)  
              Console.WriteLine(drive);  
        Console.ReadLine();  
    }  
}

Environment Variable


Similar Articles