System.Environment

Environment class allows you to obtain a number of details regarding the operating system currently hosting your .NET application using various static members. To illustrate the usefulness of System.Environment, update your Main() method to call a helper method named EnvironmentDetails().

Properties Meaning
ExitCode Gets or sets the exit code for the application
Is64BitOperatingSystem Returns a bool to represent whether the host machine is running a 64-bit OS
MachineName Gets the name of the current machine
NewLine Gets the newline symbol for the current environment
SystemDirectory Returns the full path to the system directory
UserName Returns the name of the user that started this application
Version Returns a Version object that represents the version of the .NET platform


Create EnvironmentDetails() method in class under Main() method and call this method in Main() method.

Program

 
using System;

public class ConsoleApp
{
    public static void Main(string[] args)
    {
        //implement this method in this class
        EnvironmentDetails();
        Console.ReadLine();
    }

    static void EnvironmentDetails()
    {
        //prints current OS version
        Console.WriteLine("OS version: {0}", Environment.OSVersion);
        //prints all the drives of current machine
        foreach (string drive in Environment.GetLogicalDrives())
        {
            Console.WriteLine("drive {0}", drive);
        }
        //prints no of processor in current machine
        Console.WriteLine("No of processors: {0}", Environment.ProcessorCount);
        //prints current .NET version in which program is running
        Console.WriteLine(".NET version: {0}", Environment.Version);
        //Gets or sets the exit code for the application
        Console.WriteLine("exit code: {0}", Environment.ExitCode);
        //Returns a bool to represent whether the host machine is running a 64-bit OS
        Console.WriteLine("64 bit? : {0}", Environment.Is64BitOperatingSystem);
        //Gets the name of the current machine
        Console.WriteLine("Machine name: {0}", Environment.MachineName);
        //Gets the newline symbol for the current environment
        Console.WriteLine("New line symbol: {0}", Environment.NewLine);
        //Returns the full path to the system directory
        Console.WriteLine("System directory : {0}", Environment.SystemDirectory);
        //Returns the name of the user that started this application
        Console.WriteLine("Username : {0}", Environment.UserName);
    }
}
 
 

Output


OS version: Unix 4.8.0.41
drive /
drive /usr/lib/cache
drive /etc/resolv.conf
drive /etc/hostname
drive /etc/hosts
No of processors: 2
.NET version: 4.0.30319.42000
exit code: 0
64 bit? : True
Machine name: jdoodle
New line symbol: 

System directory : 
Username : root
  

Comments

Popular posts from this blog

System.Console

Datatype and keyword