Search code examples
c#for-loopipip-addressfilestream

how to create an ip range fast as fast as possible?


how to get the fastest result i write the code below.

for (int i = 0; i < 256; i++)
             for (int j = 0; j < 256); j++)
               for (int k = 0; k < 256; k++)
                     for (int p = 0; p < 256; p++)
                     {
                         writer.WriteLine(string.Format("{0}.{1}.{2}.{3}", i, j, k, p));
                     }

but my users told me that it is dammed slow. i dont have any idea how to boost the progress. share the problem, maybe someone knows that. thanks.


Solution

  • You can try with IPAddressRange : https://www.nuget.org/packages/IPAddressRange/

    But it will still be very long if you want to get all the ipv4 range!

    var range = NetTools.IPAddressRange.Parse("192.168.0.10 - 192.168.10.20");
    System.Text.StringBuilder builder = new System.Text.StringBuilder();
      foreach (var item in range)
        {
            builder.Append(item);
        }