2012-03-24 11 views
45

Potrzebuję wiedzieć, jak uzyskać wszystkie interfejsy sieciowe z ich adresem IPv4. Albo tylko sieć bezprzewodowa i Ethernet.Jak uzyskać interfejs sieciowy i właściwy adres IPv4?

Aby uzyskać wszystkie interfejsy sieciowe szczegóły Używam tego:

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

     Console.WriteLine(ni.Name); 
    } 
} 

I żeby uzyskać wszystkie hostowane adresy IPv4 komputera:

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

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

Ale jak uzyskać interfejs sieciowy i jego prawy adres IP?

+1

Proszę przeczytać trochę ostrożniej. Zobacz [GetIPProperties] (http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getipproperties.aspx) –

+0

@JohnSaunders ok, sprawdziłem twój link przeczytaj ... i wypróbowałem to. ale nie dostałem adresu IPV4 !! jak 192.168.1.25 !! –

+3

OK, to trochę bardziej subtelne niż myślałem. Zobacz [IPGlobalProperties.GetUnicastAddresses] (http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipglobalproperties.getunicastaddresses.aspx) –

Odpowiedz

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()); 
      } 
     } 
    } 
} 

To powinno Ci dać to, co chcesz. ip.Address to adres IP, który chcesz.

+3

Można to uprościć za pomocą [this] (https: // goo .gl/rmP8RO) Zapytanie Linq. – Joseph

+0

@Joseph Twój link nie działa:/ – Felk

+1

@Felk dzięki, to jest oryginalny adres URL https://gist.github.com/anonymous/ff82643c9a004281544a – Joseph

1

Z pewną poprawę, to kod dodaje żadnego interfejsu do kombinacji:

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); 
     } 
    } 
} 

A przy wyborze jednego z nich, kod ten zwraca adres IP interfejsu:

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()); 
       } 
      } 
     } 
    } 
} 
Powiązane problemy