Freitag, 16. September 2011

Mit Visual Basic die IP-Adresse des eigenen Rechners ermitteln.

Diese Aufgabe stellte sich mir gerade. Im Internet fand ich zwei Lösungswege; der Weg über NetworkInformation erschien mir besser:

  1.  
  2. Imports N = System.Net.NetworkInformation
  3.  
  4. Module Module1
  5.  
  6.     Sub Main()
  7.         Dim aoAlleInterfaces() As N.NetworkInterface
  8.         Dim oInterface As N.NetworkInterface, oUnicastIpAdressInfoCollection As N.UnicastIPAddressInformationCollection
  9.         Dim iAnzahl As Integer , iZaehler As Integer , oUnicastIpAdressInfo As N.UnicastIPAddressInformation
  10.  
  11.         Console.WriteLine("Start" )
  12.  
  13.         aoAlleInterfaces = N.NetworkInterface.GetAllNetworkInterfaces
  14.  
  15.         For Each oInterface In aoAlleInterfaces
  16.             oUnicastIpAdressInfoCollection = oInterface.GetIPProperties().UnicastAddresses
  17.             iAnzahl = oUnicastIpAdressInfoCollection.Count
  18.             For iZaehler = 0 To iAnzahl - 1
  19.                 oUnicastIpAdressInfo = oUnicastIpAdressInfoCollection(iZaehler)
  20.                 If oUnicastIpAdressInfo.Address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
  21.                      Console.WriteLine("Treffer" )
  22.                     Console.WriteLine(oUnicastIpAdressInfo.Address.ToString)
  23.                 End If
  24.  
  25.              Next
  26.          Next
  27.  
  28.          Console.WriteLine("Ende" )
  29.         Console.ReadKey()
  30.     End Sub
  31.  
  32.  End Module

Als Vorbild diente ein Beispiel von Justus Bisser. Seine Lösung ist in C# geschrieben und nutzt LINQ; zur Anschauung habe ich es aufgedröselt.

Keine Kommentare: