2012-03-24 9 views
45

Ho bisogno di sapere come ottenere tutte le interfacce di rete con il loro indirizzo IPv4. O semplicemente wireless e Ethernet.Come faccio ad avere l'interfaccia di rete e il suo indirizzo IPv4 giusto?

Per ottenere tutte le interfacce dettagli della rete che uso questo:

foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) { 
    if(ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || 
     ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet) { 

     Console.WriteLine(ni.Name); 
    } 
} 

E per ottenere i tutti gli indirizzi IPv4 ospitate del computer:

IPAddress [] IPS = Dns.GetHostAddresses(Dns.GetHostName()); 
foreach (IPAddress ip in IPS) { 
    if (ip.AddressFamily == AddressFamily.InterNetwork) { 

     Console.WriteLine("IP address: " + ip); 
    } 
} 

ma come ottenere l'interfaccia di rete e il suo giusto indirizzo IPv4?

+1

Si prega di leggere un un po 'più attentamente. Vedere [GetIPProperties] (http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getipproperties.aspx) –

+0

@JohnSaunders bene ho controllato il tuo link leggerlo .. e provato. ma non ho ricevuto l'indirizzo IPV4 !! come 192.168.1.25 !! –

+3

Ok, è un po 'più sottile di quanto pensassi. Vedere [IPGlobalProperties.GetUnicastAddresses] (http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipglobalproperties.getunicastaddresses.aspx) –

risposta

80
foreach(NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) 
{ 
    if(ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet) 
    { 
     Console.WriteLine(ni.Name); 
     foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses) 
     { 
      if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) 
      { 
       Console.WriteLine(ip.Address.ToString()); 
      } 
     } 
    } 
} 

Questo dovrebbe ottenere quello che vuoi. ip.Address è un indirizzo IP, che si desidera.

+3

Questo può essere semplificata utilizzando [questo] (https: // goo .gl/rmP8RO) query Linq. – Joseph

+0

@Joseph il tuo link è morto:/ – Felk

+1

@Felk grazie, questo è l'URL originale https://gist.github.com/anonymous/ff82643c9a004281544a – Joseph

1

Con qualche miglioramento, questo codice aggiunge qualsiasi interfaccia a una combinazione:

private void LanSetting_Load(object sender, EventArgs e) 
{ 
    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) 
    { 
     if ((nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet) || (nic.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)) //&& (nic.OperationalStatus == OperationalStatus.Up)) 
     { 
      comboBoxLanInternet.Items.Add(nic.Description); 
     } 
    } 
} 

E quando si seleziona uno di loro, questo codice restituisce l'indirizzo IP dell'interfaccia:

private void comboBoxLanInternet_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) 
    { 
     foreach (UnicastIPAddressInformation ip in nic.GetIPProperties().UnicastAddresses) 
     { 
      if (nic.Description == comboBoxLanInternet.SelectedItem.ToString()) 
      { 
       if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) 
       { 
        MessageBox.Show(ip.Address.ToString()); 
       } 
      } 
     } 
    } 
}