Lesson #14 - The Environment class

Updated: Dec 13, 2020

The environment class is located under the ‘System’ namespace to retrieve and manage system information.

Example 1: Environment. Newline


class Program

{

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);

}


Console.ReadKey();

}

}


Result:


Example 2: Environment. CurrentDirectory

static void Main(string[] args)

{

//Get the VS working directory:

Console.ForegroundColor = ConsoleColor.Green;

Console.WriteLine(Environment.CurrentDirectory);

Console.ResetColor();

Console.ReadKey();

}


Result:


Example 3: Environment. MachineName/UserName/OSVersion/Is64/PageSize


class Program

{

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,

Environment.SystemPageSize);


Console.ReadKey();

}

}


Result:



3 views0 comments