|
お早い回答大変ありがとうございました。
参考にさせていただきまして、下記のロジックを使います。
using System.Net;
/// <summary>
/// ネットワークアドレス算出
/// </summary>
/// <param name="address">IPアドレス</param>
/// <param name="subnetMask">サブネットマスク</param>
/// <returns></returns>
public static IPAddress GetNetworkAddress(this IPAddress address, IPAddress subnetMask) {
byte[] ipAdressBytes = address.GetAddressBytes();
byte[] subnetMaskBytes = subnetMask.GetAddressBytes();
if (ipAdressBytes.Length != subnetMaskBytes.Length)
throw new ArgumentException("Lengths of IP address and subnet mask do not match.");
byte[] broadcastAddress = new byte[ipAdressBytes.Length];
for (int i = 0; i < broadcastAddress.Length; i++) {
broadcastAddress[i] = (byte)(ipAdressBytes[i] & (subnetMaskBytes[i]));
}
return new IPAddress(broadcastAddress);
}
// 使い方
IPAddress mainIp = IPAddress.Parse("192.168.0.145");
IPAddress subnetIp = IPAddress.Parse("255.255.252.0");
IPAddress networkIp;
networkIp = GetNetworkAddress(mainIp, subnetIp);
|