如何在C#中获取IP范围?

HBK*_*HBK 4 c# ip range

我是新手.我写一个程序,在C#4.0它非常适用于小范围的,但是当我去像A类IP地址范围,我的程序需要.需要大量的时间一些帮助.这里是让IP范围我代码(这不是一个好方法).有任何建议以更好的方式写作.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ipSplitt
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            string[] z = new string[4];
            int t = 0;
            string ip = "3.0.0.0";

            string[] Ip = ip.Split(new char[] { '.' });

            foreach (string m in Ip)
            {
                z[t] = m;
                t++;
            } 

            int a1, b1, c1, d1 = 0;

            a1 = Convert.ToInt32(z[0]);
            b1 = Convert.ToInt32(z[1]);
            c1 = Convert.ToInt32(z[2]);
            d1 = Convert.ToInt32(z[3]);

            string[] v = new string[4];
            string ip2 = "3.255.255.255";
            int l = 0;
            string[] Ip2 = ip2.Split(new char[] { '.' });

            foreach (string m in Ip2)
            {
                v[l] = m;
                l++;
            } 

            int a2, b2, c2, d2 = 0;

            a2 = Convert.ToInt32(v[0]);
            b2 = Convert.ToInt32(v[1]);
            c2 = Convert.ToInt32(v[2]);
            d2 = Convert.ToInt32(v[3]);

            while (d2 >= d1 || c2 > c1 || b2 > b1 || a2 > a1)
            {
                if (d1 > 255)
                {
                    d1 = 1;
                    c1++;
                }

                if (c1 > 255)
                {
                    c1 = 1;
                    b1++;
                }

                if (b1 > 255)
                {
                    b1 = 1;
                    a1++;
                }

                using (StreamWriter writer = new StreamWriter("import.txt",true))
                    writer.WriteLine(a1 + "." + b1 + "." + c1 + "." + d1);

                d1++;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Cod*_*ter 5

但是当我去A类IP地址这样的范围时,我的程序需要花费很多时间

因为您打开和关闭文件句柄255 ^ 3次.将using(Streamwriter ...)块放在外部while循环周围.