Lokale IP-Adresse anzeigen

    • VB.NET

    Es gibt 6 Antworten in diesem Thema. Der letzte Beitrag () ist von deLice.

      Lokale IP-Adresse anzeigen

      Mit diesem Code könnt ihr in z.B. Textbox eure Lokale ip-adresse anzeigen lassen.

      VB.NET-Quellcode

      1. TextBox1.Text = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList.GetValue(0).ToString
      STEAM-PROFIL // CS:GO'LER // WEBSEITE

      Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „mikeb69“ ()

      Du hast es richtig gemacht. Dir wird nur die IPv6-Adresse angezeigt. (Ist in Windows 7 und Vista(?) standardmäßig eingeschaltet)

      Auf der Seite experts-exchange.com/Programmi…s/C_Sharp/Q_25011875.html gibts eine Lösung in C# dafür. (Die Lösung wird nur angezeigt, wenn man mit Google als Referer auf die Seite geht)

      Der Löser schrieb noch Folgendes dazu:

      Typ auf experts-exchange schrieb:

      In the foreach loop, you can check the AddressFamily property of each address and return the first IPv4 one found. On my system, that's actually the third one in the list.

      Hier die konvertierte Variante:

      VB.NET-Quellcode

      1. Private Shared Function GetIP() As IPAddress
      2. Dim thisIp As IPAddress = Nothing
      3. Dim strHostName As String = Dns.GetHostName()
      4. Dim iphostentry As IPHostEntry = Dns.GetHostEntry(strHostName)
      5. ' Find host name
      6. For Each ipAddress As IPAddress In iphostentry.AddressList
      7. ' Grab the first IP addresses
      8. If ipAddress.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then
      9. thisIp = ipAddress
      10. End If
      11. Next
      12. Return thisIp
      13. End Function

      Ich habs nicht getestet.
      Von meinem iPhone gesendet
      Hay,
      doch es ist abhängig vom Betriebssystem.
      Für Xp ist der Wert anders.

      Das hier sollte gehen.

      VB.NET-Quellcode

      1. If My.Computer.Info.OSFullName.Contains("Xp") Then
      2. MessageBox.Show(System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList.GetValue(0).ToString)
      3. ElseIf My.Computer.Info.OSFullName.Contains("7") Or My.Computer.Info.OSFullName.Contains("Vista") Then
      4. MessageBox.Show(System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList.GetValue(2).ToString)
      5. End If
      die Lösung von nikeee13 ist Perfekt... mit If ipAddress.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then
      wird überprüft ob es eine IP der Version 4 ist und genau das ist die Vorraussetzung für das funktionieren(es sei denn man baut IPv6 Support ein...) in Vista+ wird die IPv6 ausgegeben...
      Ich wollte auch mal ne total überflüssige Signatur:
      ---Leer---
      Gutelaunetyp,
      dein Prüfen auf das OS ist ja grauenhaft!
      mach es so:

      VB.NET-Quellcode

      1. private function isVistaOrHigher() as boolean
      2. return environment.ocversion.version.major >= 6
      3. end function


      und auf Win7 prüfen:

      VB.NET-Quellcode

      1. private function isSevenOrHigher() as boolean
      2. return environment.ocversion.version.major = 6 andalso environment.ocversion.version.Minor >= 1) orelse environment.osVersion.version.major > 6
      3. end function
      Von meinem iPhone gesendet
      geht die lösung auch?:

      VB.NET-Quellcode

      1. Try
      2. TextBox10.Text = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList.GetValue(0).ToString
      3. If TextBox10.Text.Length > 15 Then
      4. TextBox10.Text = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList.GetValue(1).ToString
      5. End If
      6. If TextBox10.Text.Length > 15 Then
      7. TextBox10.Text = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList.GetValue(2).ToString
      8. End If
      9. If TextBox10.Text.Length > 15 Then
      10. TextBox10.Text = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList.GetValue(3).ToString
      11. End If
      12. Catch ex As Exception
      13. TextBox10.Text = "Error!"
      14. End Try



      also der sollte halt immer die textbox abfragen ob da mehr als 15 zeichen drin sind (ist bei einer lokalen ip eigentlich nie)

      also würde das auch als elegante lösung zählen :D?