Wednesday, September 11, 2013

C# - The Environment class

The ‘environment’ class located under the ‘System’ namespace, by using this class we can get the system information (Setting values and generate information).

This article will be dedicated to the main functionalities you can use while working with this class, I hope it’s going to help you when needed.


Class Property
Use case  
Environment. Newline

When you need to start a new line (equal to \n)

Environment. CurrentDirectory

When you need to get/set the VS working directory

Environment. MachineName

When you need to Retrieve the local computer name

Environment. UserName

When you need to Retrieve the current logged account

Environment. OSVersion

When you need to Retrieve to operating system version

Environment. Is64BitOperatingSystem

When you need to Retrieve the system type

Environment. SystemPageSize

When you need to Retrieve the page File size


Code examples:

Environment. Newline

static void Main(string[] args)
        {
            string[] NewLine = new string[] {"Line 1 ", "Line 2", "Line 3", "Line 4"};
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Without Using the New Line command:");
            Console.ResetColor();
            foreach (var item in NewLine)
            {
                Console.Write(item);
            }
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("\n\nUsing the New Line command:");
            Console.ResetColor();
            foreach (var item in NewLine)
            {
                Console.Write(item + Environment.NewLine);
            }
        }

Result:

Environment. CurrentDirectory
static void Main(string[] args)
        {
            //Get the VS working directory:
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(Environment.CurrentDirectory);
             Console.ResetColor();
         }
Result:

User and Machine properties
static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Operating system and user properties : ");
            Console.ResetColor();
            Console.WriteLine
(
@"Host Name = {0}
User Name = {1}
Domain Name = {2}
OS Version = {3}
System Type(X86(False) / X64(True)) = {4}
Virtual Memory size: = {5}",
Environment.MachineName ,
Environment.UserName,
Environment.UserDomainName,
Environment.OSVersion,
Environment.Is64BitOperatingSystem,
nvironment.SystemPageSize);
       }
    }
}
Result:

No comments:

Post a Comment

My Presentations