I have not programed or written code in over 10 years when I was in College. I was / am a Networks Administrator.
How, ever I find my self needing a custom interface for a DB system we recently ackwired where I work. I have been doing it in Visual Studio 2010 Express... Most of it works fine, however, I can not get the IPv4 to output to a label.txt on my forms. It always shows the IPv6, which I dont want. Can someone help me get this code? I have looked everywhere and none see to work.

Thanks a ton...
Sebastian

Recommended Answers

All 6 Replies

Show us the code that isn't working right.

Here is the code, the Host Name populates, I get IP v6 but that is all.

 Public Function GetHostEntryIPv4()
        'Display IP address of local machine
        Dim ipEntry As IPHostEntry = Dns.GetHostEntry(Environment.MachineName)
        Dim ipAddr As IPAddress() = ipEntry.AddressList
        Me.Label3.Text = ("Address: " & ipAddr(0).ToString())

        'Display DNS of local machine
        Dim myiphost As IPHostEntry = Dns.Resolve(UserDomainName)
        Dim myipaddresses() As IPAddress = myiphost.AddressList
        Dim myipaddress As IPAddress
        For Each myipaddress In myipaddresses
            Label6.Text = myipaddress.ToString()
        Next

        'Display DHCP server address of local machine
        Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
        Dim adapter As NetworkInterface
        For Each adapter In adapters
            Dim adapterProperties As IPInterfaceProperties = adapter.GetIPProperties()
            Dim addresses As IPAddressCollection = adapterProperties.DhcpServerAddresses
            If addresses.Count > 0 Then
                MessageBox.Show(adapter.Description)
                Dim address As IPAddress
                For Each address In addresses
                    Label7.Text = address.ToString()
                Next
            End If
        Next

        'Display Default Gateway of local machine
        Dim myNetworkAdapaters() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces
        Dim myadapprops As IPInterfaceProperties = Nothing
        Dim myGateways As GatewayIPAddressInformationCollection = Nothing

        For Each myNetworkAdapter As NetworkInterface In myNetworkAdapaters
            myadapprops = myNetworkAdapter.GetIPProperties
            myGateways = myadapprops.GatewayAddresses
            For Each gateway As GatewayIPAddressInformation In myGateways
                Label8.Text = gateway.Address.ToString()
            Next
        Next
    End Function

    Private Sub GetNetworkInfo() Handles Me.Shown
        Dim ipEntry As IPHostEntry = Dns.GetHostEntry(Environment.MachineName)
        Dim IpAddr As IPAddress() = ipEntry.AddressList
        Dim strHostName As String
        'Dim i As Integer
        strHostName = System.Net.Dns.GetHostName()
        Label4.Text = ("Host Name: " & strHostName)


    End Sub

Here's a quote from the MSDN document page for Dns.GetHostEntry Method (String)

When an empty string is passed as the host name, this method returns the IPv4 addresses of the local host.

I have read that already... And tried it... I cant get it to write to the label on my form.
When I try I get an error:

"Error  1   Value of type 'System.Net.IPAddress' cannot be converted to 'Integer'.  C:\Users\DaymanetALS\documents\visual studio 2010\Projects\LCARSv2\LCARSv2\Form13.vb"

Also, I cant get the other info I want, DNS, Gateway, etc)

Here is the code I tried.

 Public Sub DoGetHostEntry(hostName As [String])

        Dim host As IPHostEntry = Dns.GetHostEntry(hostName)

        Console.WriteLine("GetHostEntry(" + hostName + ") returns: ")

        Dim ip As IPAddress() = host.AddressList

        Dim index As Integer
        For index = 0 To ip.Length - 1
            Console.WriteLine(ip(index))
            Label3.Text = ("(Address: "(ip(index)))
        Next index
    End Sub

I was able to get my IPv4 to populate in Label. However, still trying to get DNS and Gateway Info.

Here is Code:

 Private Sub Getip(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Shown
        Label3.Text = ("Address: " & GetHostIP(Net.Sockets.AddressFamily.InterNetwork)) ' Show IPV4
    End Sub

    Private Function GetHostIP(ByVal af As System.Net.Sockets.AddressFamily) As String
        Dim host As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)
        Dim strRet As String = ""
        For Each ip As System.Net.IPAddress In host.AddressList
            If ip.AddressFamily = af Then
                strRet = ip.ToString
                Exit For
            End If
        Next
        Return strRet
    End Function


    Private Sub GetNetworkInfo() Handles Me.Shown
        Dim ipEntry As IPHostEntry = Dns.GetHostEntry(Environment.MachineName)
        Dim IpAddr As IPAddress() = ipEntry.AddressList
        Dim strHostName As String
        'Dim i As Integer
        strHostName = System.Net.Dns.GetHostName()
        Label4.Text = ("Host Name: " & strHostName)


    End Sub

Most of it works fine, however, I can not get the IPv4 to output to a label.txt on my forms. It always shows the IPv6, which I dont want. Can someone help me get this code? I have looked everywhere and none see to work.

I was able to get my IPv4 to populate in Label. However, still trying to get DNS and Gateway Info.

In other words your original question is solved but now you have a new one. That standard process in this case is to mark the original question solved and start a new one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.