我是新手。我在 C# 4.0 中编写了一个获取 IP 范围的程序,它适用于小范围,但是当我使用 A 类 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++;
}
}
}
}