viernes, 4 de diciembre de 2015

Obtener IP




 public static string obtenerIP()
        {
            string ip = null;
            string strHostName = null;
            IPHostEntry ipEntry = default(IPHostEntry);
            strHostName = System.Net.Dns.GetHostName();
         
            ipEntry = System.Net.Dns.GetHostEntry(strHostName);
            ip = "";

            try
            {

                if ((ipEntry.AddressList == null | ipEntry.AddressList.Count() == 0))
                {
                    ip = "No obtenida";
                }
                else
                {
                    ip = ipEntry.AddressList.Where(x => x.ToString().Contains(".")).FirstOrDefault().ToString();

                    if ((ip.Equals("")))
                    {

                        if ((ipEntry.AddressList.Count() > 2))
                        {
                            ip = ipEntry.AddressList[2].ToString();
                        }
                        else if ((ipEntry.AddressList.Count() <= 2))
                        {
                            ip = ipEntry.AddressList[1].ToString();
                        }
                    }
                }
            }
            catch (Exception)
            {
                ip = "No obtenida";
            }
            return ip;
        }


//Fuente no recordada.

No hay comentarios:

Publicar un comentario