Open In App

C# Program to Get the Machine Name or Host Name Using Environment Class

Last Updated : 30 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Environment Class provides information about the current platform and manipulates, the current platform. It is useful for getting and setting various operating system-related information. We can use it in such a way that retrieves command-line arguments information, exit codes information, environment variable settings information, contents of the call stack information and time since last system boot in milliseconds information. By just using the predefined MachineName Property we can get the machine name or the hostname using the Environment class. This property is used to find the NetBIOS name of the computer. It also throws InvalidOperationException when this property does not get the name of the computer.

Syntax:

Environment.MachineName

Return: This method returns a string that contains the machine name.

Example:

C#




// C# program to find the name of the machine
// Using Environment class
using System;
 
class GFG{
 
static public void Main()
{
     
    // Here we get the machine name
    // Using the MachineName property
    // of the Environment class
    Console.WriteLine("Machine Name is" + Environment.MachineName);
}
}


Output:

Machine Name is Check

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads