Skip to content
Related Articles
Open in App
Not now

Related Articles

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

Improve Article
Save Article
  • Last Updated : 30 Nov, 2021
Improve Article
Save Article

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.



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



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


Machine Name is Check
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!