zoukankan      html  css  js  c++  java
  • 获取本地连接ip 掩码 网关 DNS

    //获取本地连接ip 掩码 网关 DNS
                NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface bendi in interfaces)
                {
                    if (bendi.OperationalStatus != OperationalStatus.Up)
                    {
                        //break;
                    }
                    if ((bendi.Name.StartsWith("以太网") || bendi.Name.StartsWith("本地连接")) && bendi.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                    {
                        IPInterfaceProperties ip = bendi.GetIPProperties();
                        //获取Ip 掩码
                        for (int i = 0; i < ip.UnicastAddresses.Count; i++)
                        {
                            //不插网线会得到一个保留地址 169.254.126.164
                            if (ip.UnicastAddresses[i].Address.AddressFamily == AddressFamily.InterNetwork)
                            {
                                if (ip.UnicastAddresses[i].Address != null)
                                    MessageBox.Show(ip.UnicastAddresses[i].Address.ToString());
                                //如果不插网线 获取不了掩码 返回null 
                                if (ip.UnicastAddresses[i].IPv4Mask != null)
                                    MessageBox.Show(ip.UnicastAddresses[i].IPv4Mask.ToString());
                            }
                        }
                        //获取网关
                        if (ip.GatewayAddresses.Count > 0)
                            MessageBox.Show(ip.GatewayAddresses[0].Address.ToString());
                        //获取DNS     
                        //不要DnsAddresses[0].Address.ToString() 不正确 还有警告  “System.Net.IPAddress.Address”已过时:  
                        if (ip.DnsAddresses.Count > 0)
                            MessageBox.Show(ip.DnsAddresses[0].ToString());
                        //备用DNS
                        if (ip.DnsAddresses.Count > 1)
                            MessageBox.Show(ip.DnsAddresses[1].ToString());
                    }
                }
  • 相关阅读:
    CentOS 7 将 python版本升级为3.x后产生的各种问题
    CentOS 7.0 Firewall防火墙配置
    CentOS7.2+Python3x+Flask部署标准化配置流程
    CentOS 7 下安装 Nginx
    CentOS7下安装python-pip
    CentOS 7 安装字体
    centos中文目录换成英文目录
    搭建typecho个人博客和主题优化
    迭代器与生成器
    装饰器函数
  • 原文地址:https://www.cnblogs.com/xinzheng/p/12837813.html
Copyright © 2011-2022 走看看