public static IPAddress[] GetNetFindBroadcastaddr() { ArrayList mAddr = new ArrayList(); ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection nics = mc.GetInstances(); foreach (ManagementObject nic in nics) { if (Convert.ToBoolean(nic["ipEnabled"]) == true) { try { String ip = (nic["IPAddress"] as String[])[0]; String mask = (nic["IPSubnet"] as String[])[0]; String gate_way = (nic["DefaultIPGateway"] as String[])[0]; String broad_cast = LibCui.GetBroadcast(ip, mask); #if false if (LibCui.IpIs_C_Type(ip) && false) { // C 类 IP 地址--所有地址 IPAddress addr = IPAddress.Parse(ip); byte[] addrbyte = addr.GetAddressBytes(); for (int i = 1; i <= 254; i++) { addrbyte[addrbyte.Length - 1] = (byte)i; IPAddress IPOne = new IPAddress(addrbyte); mAddr.Add(IPOne); } } else { //A,B 类 IP地址 --广播地址 mAddr.Add(IPAddress.Parse(broad_cast)); } #else mAddr.Add(IPAddress.Parse(broad_cast)); #endif }catch (Exception e) { } } } IPAddress[] ip_return = (IPAddress[])mAddr.ToArray(typeof(IPAddress)); return(ip_return); }
public static IPAddress[] GetUdpBroadcastaddr() { ArrayList mAddr = new ArrayList(); ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection nics = mc.GetInstances(); foreach (ManagementObject nic in nics) { if (Convert.ToBoolean(nic["ipEnabled"]) == true) { try { String ip = (nic["IPAddress"] as String[])[0]; String mask = (nic["IPSubnet"] as String[])[0]; String gate_way = (nic["DefaultIPGateway"] as String[])[0]; String broad_cast = LibCui.GetBroadcast(ip, mask); mAddr.Add(IPAddress.Parse(broad_cast)); }catch (Exception e) { } } } IPAddress[] ip_return = (IPAddress[])mAddr.ToArray(typeof(IPAddress)); return(ip_return); }