Get ip addresses from a given range (

Posted on 2009-12-21
Last Modified: 2013-11-26
First off, let me say that I HAVE been looking for an example of how to do this, but surprisingly it is not a very popular topic\subject.

In VB.Net:

I need to get a list of IP addresses from a given range (


dim minimum_val as integer = textbox1.text
dim max_value as integer = textbox2.text

Question by:APag96
    LVL 96

    Accepted Solution

    Here is what I use:

    Sample usage:

            Dim ipAddressRange As List(Of String) = IpAddressHelper.GetAddressRange("", "")

    Public Class IpAddressHelper
        Public Shared Function GetAddressRange(ByVal startAddress As String, ByVal endAddress As String) As List(Of String)
            Dim list As New List(Of String)
            Dim n1 As Long = IpAddressToNumber(startAddress)
            Dim n2 As Long = IpAddressToNumber(endAddress)
            For i As Long = n1 To n2
            Next i
            Return list
        End Function
        Public Shared Function IpAddressToNumber(ByVal ipAddress As String) As Long
            Dim parts() As String = ipAddress.Split(".")
            Dim sum As Long
            If parts.Length <> 4 Then
                Throw New ArgumentException("Invalid IP address:  " & ipAddress)
            End If
            For i As Integer = parts.Length - 1 To 0 Step -1
                sum += (Int(parts(i)) Mod 256) * Math.Pow(256, 3 - i)
            Next i
            Return sum
        End Function
        Public Shared Function NumberToIpAddress(ByVal ipAddressNumber As Long) As String
            Dim sb As New StringBuilder()
            For i As Integer = 1 To 4
                Dim factor As Integer = (256 ^ (4 - i))
                Dim number As Long = ipAddressNumber \ factor
                ipAddressNumber -= number * factor
                sb.Append(number & ".")
            Next i
            Return sb.ToString().TrimEnd(".")
        End Function
    End Class

    Open in new window


    Author Comment

    Looks good. I'll try this out and get back to you. Thanks.

    Author Comment

    Thank you very much! That is exactily what I was looking for. 50 points for you!

    Author Closing Comment

    LVL 96

    Expert Comment

    by:Bob Learned
    "I don't need no stinkin' points"  The "Thank you very much!" means more to me.

    Author Comment

    :) You deserve the points.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    This video discusses moving either the default database or any database to a new volume.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now