How to get Host Name of your PC in C#?

string ip = Request.UserHostName;

var hostName= GetLocalHostName(ip )

public string GetLocalHostName(string ip)
        {
            IPAddress ipadd = System.Net.IPAddress.Parse(ip);
            IPHostEntry hostName = Dns.GetHostEntry(ipadd);
            var list = hostName.HostName.ToString().Split('.').ToList();
            var host = list.First();
            return host;
        }




Post a Comment

0 Comments