예제 #1
0
        public static List <NCIInfo> GetNCIInfoList()
        {
            if (s_nicInfoListCache.Count != 0)
            {
                //考虑到网卡不是一直变,用缓存提高速度
                return(s_nicInfoListCache);
            }

            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in adapters)
            {
                if (adapter.NetworkInterfaceType != NetworkInterfaceType.Ethernet)
                {
                    continue;
                }
                if (!adapter.Supports(NetworkInterfaceComponent.IPv4))
                {
                    continue;
                }

                IPInterfaceProperties ipIntProps = adapter.GetIPProperties();
                IPAddress             ipAddr     = null;
                IPAddress             ipMaskAddr = null;
                UnicastIPAddressInformationCollection ipAddrInfos = ipIntProps.UnicastAddresses;
                foreach (UnicastIPAddressInformation ipAddrInfo in ipAddrInfos)
                {
                    IPAddress ipAddrTmp = ipAddrInfo.Address;
                    if (ipAddrTmp.AddressFamily == AddressFamily.InterNetwork)
                    {
                        ipAddr     = ipAddrTmp;
                        ipMaskAddr = ipAddrInfo.IPv4Mask;
                        break;
                    }
                }

                GatewayIPAddressInformationCollection gwIps = ipIntProps.GatewayAddresses;
                IPAddress gwIp = null;
                if (gwIps.Count != 0)
                {
                    gwIp = gwIps[0].Address;
                }


                if (ipAddr != null)
                {
                    NCIInfo addr = new NCIInfo
                    {
                        Address = ipAddr,
                        Mask    = ipMaskAddr,
                        GateWay = gwIp,
                        MAC     = adapter.GetPhysicalAddress().ToString(),
                        Type    = GetNetworkInterfaceType(adapter),
                        Name    = adapter.Name,
                    };
                    s_nicInfoListCache.Add(addr);
                }
            }
            return(s_nicInfoListCache);
        }
예제 #2
0
        public static IPAddress GetBroadcastIP(string defaultMac)
        {
            NCIInfo ipv4 = NCIInfo.GetNICInfo(defaultMac);

            if (ipv4 == null)
            {
                return(null);
            }
            return(ipv4.BroadcastAddress);
        }
예제 #3
0
        public static List <IPAddress> ConvertToIP(string text)
        {
            string[]         strs = text.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            List <IPAddress> ips  = new List <IPAddress>();

            for (int i = 0; i < strs.Length; i++)
            {
                IPAddress ip = NCIInfo.Parse(strs[i]);
                if (ip != null)
                {
                    ips.Add(ip);
                }
            }
            return(ips);
        }