我是新手。我在C#4.0中編寫了一個獲取IP範圍的程序,它適用於小範圍,但是當我使用類A IP地址等範圍時,我的程序需要大量時間,需要一些幫助。是我的代碼(它不是一個好方法)。任何建議以更好的方式寫。如何在C#中獲取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++;
}
}
}
}
這不是OP正在做的事情。他不解析它們,而是打印。 – CodeCaster 2012-04-27 08:09:19
我可能已經明白他的問題了。 PS。 +1爲您的答案。在我寫回答之後才注意到它。但是當我想編輯你已經回答了它。 – SynerCoder 2012-04-27 08:10:50