Get IP Address from String host - CSharp System.Net

CSharp examples for System.Net:IP Address

Description

Get IP Address from String host

Demo Code


using System.Collections;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*w  w w. j av  a 2s  .c  o m*/

public class Main{
        // Resolve host and return only one IP
        public static string GetIPAddress(string host)
        {
            try
            {
                var addrs = Dns.GetHostEntry(host).AddressList;
                if (addrs.Length > 0)
                    return addrs[0].ToString();
            }
            catch { }

            return string.Empty;
        }
}

Related Tutorials