Get System Information Using Environment Class in C#

In this article we will learn how to get System Information Using Environment which provides the machine configuration details, program execution environment details as well as some properties which are used to break the one line into a new line which is written in the program for output.
Let us learn the definition of an array so that beginners can also understand.

What is Environment class ?

The environment class is a static class which provides the system configuration, current program execution environment as well as some properties for string manipulation such as news line. System Namespace represents the Environment Class.

The 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 the Environment variable details. Before introducing all the Environment variable details, let us go through some frequently used Environment variable details.
As we know, the Environment class is a static class. Because of this, we directly access the methods and properties with the help of the class name itself without creating an object. Let us see
 
As explained above, the Environment class is a static class which provides some properties and functions as you can see in the above image. You can access the particular property or function as follows.

 Example
     Environment.OSVersion.ToString(); //Operating system Details  
           Environment.NewLine.ToString(); //string will split into New line   
           Environment.MachineName.ToString();//Machine Name  
           Environment.Is64BitProcess.ToString();//checkes whether the system is 64 bit  
           Environment.ProcessorCount.ToString();//gets the number of process currently running on current machine  
           Environment.UserDomainName.ToString();//gets the network domain name which is currently associated with current User Computer  
           Environment.UserName.ToString();//gets the username of person who currently logged on  the windows operating System system  
           Environment.WorkingSet.ToString();//gets the Amount of physical memory mapped to the process contex  
           Environment.Exit(12);//terminates the process and gives the underlying operating system the Specified exit code  
           Environment.CurrentDirectory.ToString();//gets the full path of current working directory.  
           Environment.HasShutdownStarted.ToString();//Gets the Value whether  CLR is shutting down  
           Environment.SystemPageSize.ToString();//gets the Amount of memory for an operating system's Page file.  

To demonstrate the above properties, let us create one console application as follows:

    using System;  
    using System.Collections;  
      
    namespace UsingEnvironmentClass  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                ArrayList ar = new ArrayList();  
      
                ar.Add("OSVersion: " + Environment.OSVersion.ToString()); //Operating system Details  
                ar.Add("OSVersion Platform: " + Environment.OSVersion.Platform.ToString()); //Operating system Platform Details  
                ar.Add("OS Version: " + Environment.OSVersion.Version.ToString()); //Operating system version Details  
                ar.Add("NewLine :" + Environment.NewLine.ToString()); //string will spilt into New line   
                ar.Add("MachineName :" + Environment.MachineName.ToString());//Machine Name of the current computer  
                ar.Add("Is64BitProcess : " + Environment.Is64BitProcess.ToString());//Checks whether the OS has 64 bit process  
                ar.Add("UserDomainName: " + Environment.UserDomainName.ToString());//gets the network domain name which is currently associated with current User Computer  
                ar.Add("UserName :  " + Environment.UserName.ToString());//gets the username of person who currenlty logged on  the windows operating Sytem system  
                ar.Add("WorkingSet :" + Environment.WorkingSet.ToString());//gets the Amount of physical memory mapped to the process contex   
                ar.Add("CurrentDirectory: " + Environment.CurrentDirectory.ToString());//gets the full path of current working directory.  
                ar.Add("HasShutdownStarted:" + Environment.HasShutdownStarted.ToString());//Gets the Value whether  CLR is shutting down  
                ar.Add("SystemPageSize:" + Environment.SystemPageSize.ToString());//gets the Amount of memory for an operating system's Page file.   
      
                 
                Console.WriteLine(Environment.NewLine+"Environment Class Details"+Environment.NewLine+"---------------------------");  
                foreach (var item in ar)  
                {  
      
                    Console.WriteLine(item);  
      
                }  
                Console.ReadLine();  
      
            }  
        }  
    } 

Now run the above console application, the output will be as follows: 

 
You can also loop through the Environment class variable as you can.

IDictionary id;  
id = Environment.GetEnvironmentVariables();  
foreach (DictionaryEntry DE in id)  
{  
  
    string VariableName =DE.Key.ToString();  
    VariableName += "  =    ";  
    VariableName += DE.Value.ToString();  
    Response.Write("<li>" + VariableName +',' +"</li>");  
  
}  

The output of the above program will be as follows:


Summary

I hope this article is useful for all readers. If you have any suggestion regarding this article, then please contact me.

Post a Comment

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode