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
- Environment.OSVersion.ToString();
- Environment.NewLine.ToString();
- Environment.MachineName.ToString();
- Environment.Is64BitProcess.ToString();
- Environment.ProcessorCount.ToString();
- Environment.UserDomainName.ToString();
- Environment.UserName.ToString();
- Environment.WorkingSet.ToString();
- Environment.Exit(12);
- Environment.CurrentDirectory.ToString();
- Environment.HasShutdownStarted.ToString();
- Environment.SystemPageSize.ToString();
To demonstrate the above properties lets us create the one console application,as
- using System;
- using System.Collections;
-
- namespace UsingEnvironmentClass
- {
- class Program
- {
- static void Main(string[] args)
- {
- ArrayList ar = new ArrayList();
-
- ar.Add("OSVersion: " + Environment.OSVersion.ToString());
- ar.Add("OSVersion Platform: " + Environment.OSVersion.Platform.ToString());
- ar.Add("OS Version: " + Environment.OSVersion.Version.ToString());
- ar.Add("NewLine :" + Environment.NewLine.ToString());
- ar.Add("MachineName :" + Environment.MachineName.ToString());
- ar.Add("Is64BitProcess : " + Environment.Is64BitProcess.ToString());
- ar.Add("UserDomainName: " + Environment.UserDomainName.ToString());
- ar.Add("UserName : " + Environment.UserName.ToString());
- ar.Add("WorkingSet :" + Environment.WorkingSet.ToString());
- ar.Add("CurrentDirectory: " + Environment.CurrentDirectory.ToString());
- ar.Add("HasShutdownStarted:" + Environment.HasShutdownStarted.ToString());
- ar.Add("SystemPageSize:" + Environment.SystemPageSize.ToString());
-
-
- 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
You can also loop through the Environment class variable as
- 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
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.