How to Get HostName in C#/CSharp

To get host name of the local computer, we make use of GetHostName() method which resides in Dns class.  Dns class is part of System.Net space.  Example is given below.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
using System.Net;
namespace Hello_World
{
class Program
{
static void Main(string[] args)
{
string hostname = Dns.GetHostName();
Console.WriteLine(hostname);
}
}
}
using System; using System.Net; namespace Hello_World { class Program { static void Main(string[] args) { string hostname = Dns.GetHostName(); Console.WriteLine(hostname); } } }
using System;
using System.Net;

namespace Hello_World
{

    class Program
    {
        static void Main(string[] args)
        {
            string hostname = Dns.GetHostName();

            Console.WriteLine(hostname);
        }
    }
}