编写一个类来存储/验证类似的东西很简单192.168.0.0/16
,但是我很好奇.NET中是否存在本机类型?我想它会像以下一样工作IPAddress
:
CIDR subnet = CIDR.Parse("192.168.0.0/16");
基本上,它只需要确保您使用IPv4或IPv6地址,然后确定您指定的位数对该类型有效.
您可以使用CodePlex中的代码执行此操作:
http://ipnetwork.codeplex.com/
IPNetwork ipnetwork = IPNetwork.Parse("192.168.168.100/24"); Console.WriteLine("Network : {0}", ipnetwork.Network); Console.WriteLine("Netmask : {0}", ipnetwork.Netmask); Console.WriteLine("Broadcast : {0}", ipnetwork.Broadcast); Console.WriteLine("FirstUsable : {0}", ipnetwork.FirstUsable); Console.WriteLine("LastUsable : {0}", ipnetwork.LastUsable); Console.WriteLine("Usable : {0}", ipnetwork.Usable); Console.WriteLine("Cidr : {0}", ipnetwork.Cidr);
产量
Network : 192.168.168.0 Netmask : 255.255.255.0 Broadcast : 192.168.168.255 FirstUsable : 192.168.168.1 LastUsable : 192.168.168.254 Usable : 254 Cidr : 24