コード例 #1
0
ファイル: NetworkUtil.cs プロジェクト: limingnihao/Net
        /// <summary>
        /// 获取当前本地连接列表,根据网络接口类型,和操作状态
        /// </summary>
        /// <param name="type">网络接口类型</param>
        /// <param name="ipEnabled">网络接口的操作状态</param>
        public static List<NetworkVO> GetNetworkList(NetworkInterfaceType type, OperationalStatus status)
        {
            List<NetworkVO> list = new List<NetworkVO>();
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in adapters)
            {
                //过滤网络接口类型
                if (!NetworkInterfaceType.Unknown.Equals(type) && !type.Equals(adapter.NetworkInterfaceType))
                {
                    logger.Debug("跳过的其他类型网络,name=" + adapter.Name + ", NetworkInterfaceType=" + adapter.NetworkInterfaceType + ", OperationalStatus=" + adapter.OperationalStatus);
                    continue;
                }
                //过滤网络接口的操作状态
                if (!status.Equals(adapter.OperationalStatus))
                {
                    logger.Debug("跳过的不是上行网络,name=" + adapter.Name + ", NetworkInterfaceType=" + adapter.NetworkInterfaceType + ", OperationalStatus=" + adapter.OperationalStatus);
                    continue;
                }
                NetworkVO vo = new NetworkVO();
                vo.IpEnabled = true;
                IPInterfaceProperties property = adapter.GetIPProperties();
                vo.DnsHostName = Dns.GetHostName();//本机名
                vo.Name = adapter.Name;
                vo.Description = adapter.Description;
                vo.Speed = adapter.Speed;

                //macAddress
                if (adapter.GetPhysicalAddress() != null && adapter.GetPhysicalAddress().ToString().Length > 0)
                {
                    char[] mac = adapter.GetPhysicalAddress().ToString().ToCharArray();
                    vo.MacAddress = mac[0] + mac[1] + "-" + mac[2] + mac[3] + "-" + mac[4] + mac[5] + "-" + mac[6] + mac[7] + "-" + mac[8] + mac[9] + "-" + mac[10] + mac[11];
                }

                //ipAddress subnetMask 
                if (property.UnicastAddresses != null && property.UnicastAddresses.Count > 0)
                {
                    foreach (UnicastIPAddressInformation ip in property.UnicastAddresses)
                    {
                        if (ip.Address.AddressFamily.Equals(AddressFamily.InterNetwork))
                        {
                            if (ip.Address != null)
                            {
                                vo.Address = ip.Address.ToString();
                            }
                            if (ip.IPv4Mask != null)
                            {
                                vo.SubnetMask = ip.IPv4Mask.ToString();
                            }
                        }
                    }
                }

                // gateway
                if (property.GatewayAddresses != null && property.GatewayAddresses.Count > 0)
                {
                    foreach (GatewayIPAddressInformation uip in property.GatewayAddresses)
                    {
                        if (uip.Address.AddressFamily.Equals(AddressFamily.InterNetwork))
                        {
                            vo.Gateway = uip.Address.ToString();
                        }
                    }
                }

                // dns server
                if (property.DnsAddresses != null && property.DnsAddresses.Count > 0)
                {
                    vo.DnsServers = new List<string>();
                    foreach (IPAddress ip in property.DnsAddresses)
                    {
                        if (ip.AddressFamily.Equals(AddressFamily.InterNetwork))
                        {
                            vo.DnsServers.Add(ip.ToString());
                        }
                    }
                }

                // dhcp server
                if (property.DhcpServerAddresses != null && property.DhcpServerAddresses.Count > 0)
                {
                    foreach (IPAddress ip in property.DhcpServerAddresses)
                    {
                        if (ip.AddressFamily.Equals(AddressFamily.InterNetwork))
                        {
                            vo.DhcpServer = ip.ToString();
                            vo.DhcpEnabled = true;
                        }
                    }
                }
                else
                {
                    vo.DhcpEnabled = false;
                }
                list.Add(vo);
            }
            return list;
        }
コード例 #2
0
ファイル: NetworkUtil.cs プロジェクト: limingnihao/Net
        /// <summary>
        /// 使用win32方式获取网络连接。获取不到名称。
        /// </summary>
        /// <returns></returns>
        public static List<NetworkVO> GetNetworkListWin32()
        {
            List<NetworkVO> list = new List<NetworkVO>();
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                PropertyDataCollection coll = mo.Properties;
                bool ipEnabled = (bool)mo["IPEnabled"];
                if (!ipEnabled)
                {
                    continue;
                }
                string description = (string)mo["Description"];
                string dnsHostName = (string)mo["DNSHostName"];
                string macAddress = (string)mo["MACAddress"];
                string dhcpServer = (string)mo["DHCPServer"];
                bool dhcpEnabled = (bool)mo["DHCPEnabled"];

                string[] addresses = (string[])mo["IPAddress"];
                string[] subnets = (string[])mo["IPSubnet"];
                string[] gateways = (string[])mo["DefaultIPGateway"];
                string[] dnses = (string[])mo["DNSServerSearchOrder"];

                NetworkVO v = new NetworkVO();
                v.IpEnabled = ipEnabled;
                if (addresses != null && addresses.Length > 0 && addresses[0] != null)
                {
                    v.Address = addresses[0];
                }
                if (subnets != null && subnets.Length > 0 && subnets[0] != null)
                {
                    v.SubnetMask = subnets[0];
                }
                if (gateways != null && gateways.Length > 0 && gateways[0] != null)
                {
                    v.Gateway = gateways[0];
                }
                v.DnsServers = new List<string>();
                foreach (string dns in dnses)
                {
                    v.DnsServers.Add(dns);
                }
                v.Description = description;
                v.MacAddress = macAddress;
                v.DnsHostName = dnsHostName;
                v.DhcpServer = dhcpServer;
                v.DhcpEnabled = dhcpEnabled;
                list.Add(v);
            }
            return list;
        }