Environment Class in C#

 Background
 
In this article we will learn about the Environment  class of C#,which provides the machine configuration details,Program execution Environment details as well as some properties which is used to break the one line into new line which is written in the program for output.
Let us learn the definition of array so beginners can also understand.
 
What is Environment class ?
 
Environment class is static class which Provides the system configuration,Current program execution Environment as wel some properties for string manipulation such as news line,System Namespace represents the Environment Class.
Environment class is a combination of functions and Properties which represent the Environment variable details using IDictionary in the form of key,value pairs.
we can also loop through  Dictionary to get all Environment variable details,before introducing all Environment variable,let us go through some frequently used Environment variable details.
as we Environment class is the static class because of this we directly access the methods and properties with help of class name itself without creating object.
Let us see
As explained above defination ,Environment class is the static class which provides the some properties and function as you can see in the above Image.
You can access the particular property or function as
 
 Example
  1.        Environment.OSVersion.ToString(); //Operating system Details  
  2.        Environment.NewLine.ToString(); //string will split into New line   
  3.        Environment.MachineName.ToString();//Machine Name  
  4.        Environment.Is64BitProcess.ToString();//checkes whether the system is 64 bit  
  5.        Environment.ProcessorCount.ToString();//gets the number of process currently running on current machine  
  6.        Environment.UserDomainName.ToString();//gets the network domain name which is currently associated with current User Computer  
  7.        Environment.UserName.ToString();//gets the username of person who currently logged on  the windows operating System system  
  8.        Environment.WorkingSet.ToString();//gets the Amount of physical memory mapped to the process contex  
  9.        Environment.Exit(12);//terminates the process and gives the underlying operating system the Specified exit code  
  10.        Environment.CurrentDirectory.ToString();//gets the full path of current working directory.  
  11.        Environment.HasShutdownStarted.ToString();//Gets the Value whether  CLR is shutting down  
  12.        Environment.SystemPageSize.ToString();//gets the Amount of memory for an operating system's Page file.  
To demonstrate the  above properties lets us create the one console application,as
  1. using System;  
  2. using System.Collections;  
  3.   
  4. namespace UsingEnvironmentClass  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             ArrayList ar = new ArrayList();  
  11.   
  12.             ar.Add("OSVersion: " + Environment.OSVersion.ToString()); //Operating system Details  
  13.             ar.Add("OSVersion Platform: " + Environment.OSVersion.Platform.ToString()); //Operating system Platform Details  
  14.             ar.Add("OS Version: " + Environment.OSVersion.Version.ToString()); //Operating system version Details  
  15.             ar.Add("NewLine :" + Environment.NewLine.ToString()); //string will spilt into New line   
  16.             ar.Add("MachineName :" + Environment.MachineName.ToString());//Machine Name of the current computer  
  17.             ar.Add("Is64BitProcess : " + Environment.Is64BitProcess.ToString());//Checks whether the OS has 64 bit process  
  18.             ar.Add("UserDomainName: " + Environment.UserDomainName.ToString());//gets the network domain name which is currently associated with current User Computer  
  19.             ar.Add("UserName :  " + Environment.UserName.ToString());//gets the username of person who currenlty logged on  the windows operating Sytem system  
  20.             ar.Add("WorkingSet :" + Environment.WorkingSet.ToString());//gets the Amount of physical memory mapped to the process contex   
  21.             ar.Add("CurrentDirectory: " + Environment.CurrentDirectory.ToString());//gets the full path of current working directory.  
  22.             ar.Add("HasShutdownStarted:" + Environment.HasShutdownStarted.ToString());//Gets the Value whether  CLR is shutting down  
  23.             ar.Add("SystemPageSize:" + Environment.SystemPageSize.ToString());//gets the Amount of memory for an operating system's Page file.   
  24.   
  25.              
  26.             Console.WriteLine(Environment.NewLine+"Environment Class Details"+Environment.NewLine+"---------------------------");  
  27.             foreach (var item in ar)  
  28.             {  
  29.   
  30.                 Console.WriteLine(item);  
  31.   
  32.             }  
  33.             Console.ReadLine();  
  34.   
  35.         }  
  36.     }  

Now run the  above  console application,the output will be as 
 
 
 
You can also loop through the  Environment class variable as
  1. IDictionary id;  
  2. id = Environment.GetEnvironmentVariables();  
  3. foreach (DictionaryEntry DE in id)  
  4. {  
  5.   
  6.     string VariableName =DE.Key.ToString();  
  7.     VariableName += "  =    ";  
  8.     VariableName += DE.Value.ToString();  
  9.     Response.Write("<li>" + VariableName +',' +"</li>");  
  10.   
  11. }       
The output of the above program will be as
 
 

From the above output its also clear that Environment Variables details contains in the form of key -value pairs.
 
Note:
  • Download the Zip file from the attachment for the full source code of the application.
Summary

In the preceding article, I have briefly explained the use of a Environment class to make them understandable to beginners and newcomers. If you have any suggestion regarding this article then please contact me.
 

Up Next
    Ebook Download
    View all
    Learn
    View all