2012-02-07 170 views
0

我需要使用VB.Net通过一个IP地址范围来遍历一个优雅的方式,当输入会来我的应用程序在此格式的字符串:迭代虽然IP的地址范围

192.168.100.8-10 

这个范围将包括3个地址:

192.168.100.8, 192.168.100.9, 192.168.100.10. 

我发现在C#中使用的IP地址类,我大概可以转换为VB的解决方案,但它似乎是太多的代码,我需要做的。我肯定可以使用一堆字符串解析函数,但我希望有人已经有了一个简单的方法来完成这个任务。

+1

这似乎是一个相对简单的编程练习,只要抓住通过RE的最后一位,解析出“ - ”,以获得最大/最小值,这样做一个小错误检查(x < y && both > 0和<255),你就完成了。 – KevinDTimm 2012-02-07 18:57:46

回答

0

这是一个解决方案。它甚至会使用泛型列表更容易...

Dim arrFinalIpList() As String 

    Dim strIP As String = "192.168.100.8-10" 

    Dim arrIP() As String = strIP.Split(".") 
    Dim strPrefix As String = arrIP(0) & "." & arrIP(1) & "." & arrIP(2) & "." 
    Dim arrMinAndMax() As String = arrIP(3).Split("-") 

    Dim intCursor As Integer = 0 

    For intCursor = CInt(arrMinAndMax(0)) To CInt(arrMinAndMax(1)) 

     If arrFinalIpList Is Nothing Then 

      ReDim arrFinalIpList(0) 
      arrFinalIpList(0) = strPrefix & intCursor.ToString() 

     Else 

      ReDim Preserve arrFinalIpList(arrFinalIpList.Count) 
      arrFinalIpList(arrFinalIpList.Count - 1) = strPrefix & intCursor.ToString() 

     End If 

    Next