码迷,mamicode.com
首页 > 其他好文 > 详细

csharp:获取 DNS、网关、子网掩码、IP

时间:2015-06-24 18:40:42      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

/// <summary>
    /// DNS、网关、子网掩码、IP
    /// 涂聚文 2015
    /// </summary>
    public class IPAddressString
    {
        /// <summary>
        /// IP地址
        /// </summary>
        private string _localAddress;
        /// <summary>
        /// 子网掩码 
        /// </summary>
        private string _ipSubnet;
        /// <summary>
        /// 默认网关
        /// </summary>
        private string _defaultIPGateway;
        /// <summary>
        /// DNS
        /// </summary>
        private string _firstDNS;
        /// <summary>
        /// 备用DNS
        /// </summary>
        private string _secondDNS;
        /// <summary>
        /// 
        /// </summary>
        private string _hostName;
        /// <summary>
        /// 
        /// </summary>
        private string _macname;

        /// <summary>
        /// IP地址
        /// </summary>
        public string LocalAddress
        {
            get { return _localAddress; }
            set { _localAddress = value; }
        }

        /// <summary>
        /// 子网掩码 
        /// </summary>
        public string IpSubnet
        {
            get { return _ipSubnet; }
            set { _ipSubnet = value; }
        }
        /// <summary>
        /// 默认网关
        /// </summary>
        public string DefaultIPGateway
        {
            get { return _defaultIPGateway; }
            set { _defaultIPGateway = value; }
        }
        /// <summary>
        /// DNS
        /// </summary>
        public string FirstDNS
        {
            get { return _firstDNS; }
            set { _firstDNS = value; }
        }
        /// <summary>
        /// 备用DNS
        /// </summary>
        public string SecondDNS
        {
            get { return _secondDNS; }
            set { _secondDNS = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string HostName
        {
            get { return _hostName; }
            set { _hostName = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Macname
        {
            get { return _macname; }
            set { _macname = value; }
        }
    }

/// <summary>
        ///获取 DNS、网关、子网掩码、IP
        ///GEOVIN DU
        /// </summary>
        /// <returns></returns>
        public IPAddressString getIp()
        {
            IPAddressString ip = new IPAddressString();
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection nics = mc.GetInstances();
            foreach (ManagementObject nic in nics)
            {
                if (Convert.ToBoolean(nic["ipEnabled"]) == true)
                {
                    // Get IP,SubNetMask,Gateway
                   ip.LocalAddress = (nic["IPAddress"] as string[])[0];
                   ip.IpSubnet = (nic["IPSubnet"] as string[])[0];
                   ip.DefaultIPGateway = (nic["DefaultIPGateway"] as string[])[0];
                   ip.Macname = nic["MacAddress"].ToString();
                   ip.HostName = Dns.GetHostName();//计算机名
                }
            }

            // Get DNS
            NetworkInterface[] ifs = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface netif in ifs)
            {
                IPInterfaceProperties properties = netif.GetIPProperties();
                IPAddressCollection dnses = properties.DnsAddresses;
                if (dnses.Count > 0)
                {
                    int i = 0;
                    foreach (IPAddress ipAddr in dnses)
                    {
                        // Set DNS To DNS TextBox
                        if (i == 0)
                            ip.FirstDNS = ipAddr.ToString();
                        else
                            ip.SecondDNS = ipAddr.ToString();

                        i++;
                    }
                    break;
                }
            }

            return ip;
        }

  测试

 //1
            //IPAddress ip = Dns.GetHostAddresses(Dns.GetHostName()).Where(address => address.AddressFamily == AddressFamily.InterNetwork).First();
            //MessageBox.Show(ip.ToString());


            //2
            //IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
            //foreach (IPAddress addr in localIPs)
            //{
            //    if (addr.AddressFamily == AddressFamily.InterNetwork)
            //    {
            //        MessageBox.Show(addr.ToString());
            //    }               
               
            //}

            //3.
            //IPAddress ip = GetIPAddress("dusystem.com");
            //MessageBox.Show(ip.ToString());

            //4.
            //List<string> ips = GetIPAddress(Dns.GetHostName());
            //foreach (string s in ips)
            //{
            //    MessageBox.Show(s.ToString());
            //}
            
            //5
            IPAddressString ip = getIp();
            MessageBox.Show(ip.IpSubnet);

  

csharp:获取 DNS、网关、子网掩码、IP

标签:

原文地址:http://www.cnblogs.com/geovindu/p/4598144.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!