Feedback

VB - IP-Addressen aus Registry auslesen

Veröffentlicht von am 12/17/2006
(2 Bewertungen)
Manchmal ist es notwendig, dass man die in der Registry eingetragenen IP-Addressen aller vorhandenen Adapter auslesen muss.

Das nachfolgenden Snippet ermöglicht dies und gibt die Daten als String (xxx.xxx.xxx.xxx) zurück
    Public Shared Function GetIPAddresses(ByVal adapter As String) As String()
        Dim oBuffer As New ArrayList()
        Dim sInterface As String
        Dim arrInterface As String()
        Dim sIPAddress As String
        Dim arrIPAddress As String()
        Dim bDHCP As Boolean
        Dim strBaseKey As String = "SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"
        Dim objRootKey As Microsoft.Win32.RegistryKey
        Dim objKey As Microsoft.Win32.RegistryKey
        Dim Registry As Microsoft.Win32.Registry = Nothing



        objRootKey = Registry.LocalMachine.OpenSubKey(strBaseKey, False)

        If objRootKey Is Nothing Then
            Return oBuffer.ToArray(Type.GetType("System.String"))
            Exit Function
        End If
        arrInterface = objRootKey.GetSubKeyNames()

        For Each sInterface In arrInterface



            objKey = Registry.LocalMachine.OpenSubKey(strBaseKey & sInterface & "\", False)
            ''Make sure that we got a key!
            If Not (objKey Is Nothing) Then
                'Pruft ob DHCP eingeschaltet ist
                'wenn nicht, werden alle vorghanden IP addressen geladen
                bDHCP = objKey.GetValue("EnableDCHP", False)
                If bDHCP Then
                    'Einzelne IP address auslesen
                    sIPAddress = objKey.GetValue("DhcpIPAddress", "")
                    'Pruefung ob gueltige IP
                    If (sIPAddress.Length > 0) And (sIPAddress <> "0.0.0.0") Then
                        oBuffer.Add(sIPAddress)
                    End If
                Else

                    For Each oName As Object In objKey.GetValueNames
                        'MsgBox(oName.ToString())
                        If oName.ToString.ToLower = "ipaddress" Then
                            'Lesen und array erstellen
                            arrIPAddress = objKey.GetValue(oName, "")
                            'Pruefung ob gueltige IP
                            For Each sIPAddress In arrIPAddress
                                If (sIPAddress.Length > 0) And (sIPAddress <> "0.0.0.0") Then
                                    oBuffer.Add(sIPAddress)
                                End If
                            Next
                        End If
                    Next
                End If
            End If
        Next
        Registry.LocalMachine.Close()

        Return oBuffer.ToArray(Type.GetType("System.String"))

    End Function
Abgelegt unter Netzwerk, IP, Rwegistry.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!