Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 30 Nov, 2021

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!