程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> csharp:獲取 DNS、網關、子網掩碼、IP,csharpdns

csharp:獲取 DNS、網關、子網掩碼、IP,csharpdns

編輯:C#入門知識

csharp:獲取 DNS、網關、子網掩碼、IP,csharpdns


/// <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);

  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved