分享免费的编程资源和教程

网站首页 > 技术教程 正文

C#得到网关和DNS地址 c#本机ip

goqiw 2024-10-07 11:52:29 技术教程 12 ℃ 0 评论

想控制学生机上网,做对了题就直接把网络连通,先解决得到的问题,下一步再看如何更改它们:

/// <summary>
        /// 得到网关
        /// </summary>
        /// <returns></returns>
        public string GetGateway()
        {
            try
            {
                //网关地址
                string strGateway = "";
                //获取所有网卡
                NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                //遍历数组
                foreach (var netcard in nics)
                {
                    //单个网卡的IP对象
                    IPInterfaceProperties ip = netcard.GetIPProperties();
                    //获取该IP对象的网关
                    GatewayIPAddressInformationCollection gateways = ip.GatewayAddresses;
                    foreach (var gwg in gateways)
                    {
                        //如果能够Ping通网关
                        if (Ping(gwg.Address.ToString()) == "true")
                        {
                            //得到网关地址
                            strGateway = gwg.Address.ToString();
                            //跳出循环
                            break;
                        }
                    }

                    //如果已经得到网关地址
                    if (strGateway.Length > 0)
                    {
                        //跳出循环
                        break;
                    }
                }

                //返回网关地址
                return strGateway;
            }
            catch (Exception ErrMsg)
            {
                Error((int)FunctionNames.HardWare__NetCard__GetGateway, ErrMsg.ToString());
                return ErrMsg.ToString();
            }
        }

        /// <summary>
        /// 得到dns地址
        /// </summary>
        /// <returns></returns>
        public string[] GetDns()
        {
            try
            {
                ArrayList dnsstrs = new ArrayList();
                string dnsstr = "";
                NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                //遍历数组
                foreach (var netcard in nics)
                {
                    //单个网卡的IP对象
                    IPInterfaceProperties ip = netcard.GetIPProperties();
                    //获取该IP对象的网关
                    IPAddressCollection dnss = ip.DnsAddresses;
                    foreach (var dns in dnss)
                    {
                        //如果能够Ping通DNS
                        if (Ping(dns.ToString()) == "true")
                        {
                            //得到DNS地址
                            dnsstr = dns.ToString();
                            dnsstrs.Add(dnsstr);
                        }
                    }

                    //如果已经得到网关地址
                    if (dnsstr.Length > 0)
                    {
                        //跳出循环
                        break;
                    }
                }

                return (string[])dnsstrs.ToArray(typeof(string));
            }
            catch (Exception ErrMsg)
            {
                Error((int)FunctionNames.HardWare__NetCard__GetDNS, ErrMsg.ToString());
                return ErrInfo(ErrMsg.ToString());
            }
        }

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表