none
Sacar IP LAN de mi tarjeta ethernet RRS feed

  • Pregunta

  • Mi duda es como puedo buscar o sacar mi ip de ethernet logre sacar todas las ip de mi pc en lista

    string strHostName = Dns.GetHostName();

                IPHostEntry iphostentry = Dns.GetHostByName(strHostName);

                foreach (IPAddress ipaddress in iphostentry.AddressList)

                {

                    Console.WriteLine(ipaddress.ToString());

                }

    Ahi sale la de ethernet me salen todas pero solo quiero que me aparesca la de mi tarjeta de ethernet y no todas, espero que me puedan ayudar

    solo quiero la Ethernet Adapter Local Area Connection

    Saludos

    jueves, 3 de marzo de 2011 23:33

Respuestas

  • Saludos,

    La solucion  para esto requiere un poco mas de codigo, mira el siguiente enlace ,  la respuesta #2 tiene la respuesta que buscas

    http://stackoverflow.com/questions/3690472/what-is-my-internet-accessing-ip

    mira el codigo aqui

    namespace ConsoleApplication1
    {
      using System;
      using System.Collections.Generic;
      using System.Net;
      using System.Net.NetworkInformation;
      using System.Net.Sockets;
    
      class Program
      {
        public static List<IPAddress> GetInternetIPAddressUsingSocket(string internetHostName, int port)
        {
          // get remote host IP. Will throw SocketExeption on invalid hosts
          IPHostEntry remoteHostEntry = Dns.GetHostEntry(internetHostName);
          IPEndPoint remoteEndpoint = new IPEndPoint(remoteHostEntry.AddressList[0], port);
    
          // Get all locals IP
          IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
    
          var internetIPs = new List<IPAddress>();
          // try to connect using each IP       
          foreach (IPAddress ip in hostEntry.AddressList) {
            IPEndPoint localEndpoint = new IPEndPoint(ip, 80);
    
            bool endpointIsOK = true;
            try {
              using (Socket socket = new Socket(localEndpoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) {
                socket.Connect(remoteEndpoint);
              }
            }
            catch (Exception) {
              endpointIsOK = false;
            }
    
            if (endpointIsOK) {
              internetIPs.Add(ip); // or you can return first IP that was found as single result.
            }
          }
    
          return internetIPs;
        }
    
        public static HashSet <IPAddress> GetInternetIPAddress(string internetHostName)
        {
          // get remote IPs
          IPHostEntry remoteMachineHostEntry = Dns.GetHostEntry(internetHostName);
    
          var internetIPs = new HashSet<IPAddress>();
    
          // connect and search for local IP
          WebRequest request = HttpWebRequest.Create("http://" + internetHostName);
          using (WebResponse response = request.GetResponse()) {
            IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
            TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
            response.Close();
    
            foreach (TcpConnectionInformation t in connections) {
              if (t.State != TcpState.Established) {
                continue;
              }
    
              bool isEndpointFound = false;
              foreach (IPAddress ip in remoteMachineHostEntry.AddressList) {
                if (ip.Equals(t.RemoteEndPoint.Address)) {
                  isEndpointFound = true;
                  break;
                }
              }
    
              if (isEndpointFound) {
                internetIPs.Add(t.LocalEndPoint.Address);
              }
            }
          }
    
          return internetIPs;
        }
    
    
        static void Main(string[] args)
        {
    
          List<IPAddress> internetIP = GetInternetIPAddressUsingSocket("google.com", 80);
          foreach (IPAddress ip in internetIP) {
            Console.WriteLine(ip);
          }
    
          Console.WriteLine("======");
    
          HashSet<IPAddress> internetIP2 = GetInternetIPAddress("google.com");
          foreach (IPAddress ip in internetIP2) {
            Console.WriteLine(ip);
          }
    
          Console.WriteLine("Press any key");
          Console.ReadKey(true);
        }
      }
    }
    


    Live like you'll die tomorrow, learn like you'll live forever. Blog
    jueves, 3 de marzo de 2011 23:43
  • Mira este thread , hacen exactamente lo que pides, pero hay que acceder al registro 

    http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/c3f3355f-b134-41fa-b93f-bb2b7f9c5855/


    Live like you'll die tomorrow, learn like you'll live forever. Blog
    jueves, 3 de marzo de 2011 23:57

Todas las respuestas

  • Saludos,

    La solucion  para esto requiere un poco mas de codigo, mira el siguiente enlace ,  la respuesta #2 tiene la respuesta que buscas

    http://stackoverflow.com/questions/3690472/what-is-my-internet-accessing-ip

    mira el codigo aqui

    namespace ConsoleApplication1
    {
      using System;
      using System.Collections.Generic;
      using System.Net;
      using System.Net.NetworkInformation;
      using System.Net.Sockets;
    
      class Program
      {
        public static List<IPAddress> GetInternetIPAddressUsingSocket(string internetHostName, int port)
        {
          // get remote host IP. Will throw SocketExeption on invalid hosts
          IPHostEntry remoteHostEntry = Dns.GetHostEntry(internetHostName);
          IPEndPoint remoteEndpoint = new IPEndPoint(remoteHostEntry.AddressList[0], port);
    
          // Get all locals IP
          IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
    
          var internetIPs = new List<IPAddress>();
          // try to connect using each IP       
          foreach (IPAddress ip in hostEntry.AddressList) {
            IPEndPoint localEndpoint = new IPEndPoint(ip, 80);
    
            bool endpointIsOK = true;
            try {
              using (Socket socket = new Socket(localEndpoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) {
                socket.Connect(remoteEndpoint);
              }
            }
            catch (Exception) {
              endpointIsOK = false;
            }
    
            if (endpointIsOK) {
              internetIPs.Add(ip); // or you can return first IP that was found as single result.
            }
          }
    
          return internetIPs;
        }
    
        public static HashSet <IPAddress> GetInternetIPAddress(string internetHostName)
        {
          // get remote IPs
          IPHostEntry remoteMachineHostEntry = Dns.GetHostEntry(internetHostName);
    
          var internetIPs = new HashSet<IPAddress>();
    
          // connect and search for local IP
          WebRequest request = HttpWebRequest.Create("http://" + internetHostName);
          using (WebResponse response = request.GetResponse()) {
            IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
            TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
            response.Close();
    
            foreach (TcpConnectionInformation t in connections) {
              if (t.State != TcpState.Established) {
                continue;
              }
    
              bool isEndpointFound = false;
              foreach (IPAddress ip in remoteMachineHostEntry.AddressList) {
                if (ip.Equals(t.RemoteEndPoint.Address)) {
                  isEndpointFound = true;
                  break;
                }
              }
    
              if (isEndpointFound) {
                internetIPs.Add(t.LocalEndPoint.Address);
              }
            }
          }
    
          return internetIPs;
        }
    
    
        static void Main(string[] args)
        {
    
          List<IPAddress> internetIP = GetInternetIPAddressUsingSocket("google.com", 80);
          foreach (IPAddress ip in internetIP) {
            Console.WriteLine(ip);
          }
    
          Console.WriteLine("======");
    
          HashSet<IPAddress> internetIP2 = GetInternetIPAddress("google.com");
          foreach (IPAddress ip in internetIP2) {
            Console.WriteLine(ip);
          }
    
          Console.WriteLine("Press any key");
          Console.ReadKey(true);
        }
      }
    }
    


    Live like you'll die tomorrow, learn like you'll live forever. Blog
    jueves, 3 de marzo de 2011 23:43
  • el problema ceria que tendria que estar conectado a internet y aparte si te regresa toda la lista :(
    jueves, 3 de marzo de 2011 23:54
  • Mira este thread , hacen exactamente lo que pides, pero hay que acceder al registro 

    http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/c3f3355f-b134-41fa-b93f-bb2b7f9c5855/


    Live like you'll die tomorrow, learn like you'll live forever. Blog
    jueves, 3 de marzo de 2011 23:57
  • Pues es algo parecido que mi otro codigo nomas que este con mas especificaciones pero talves este pueda servirme

    Saludos gяєgσяу

    viernes, 4 de marzo de 2011 0:19