C#实现得到本机IP以及网关地址

        // 得到本机IP      
        private string GetLocalIP() 
        {
            //本机IP地址
            string strLocalIP = "";
            //得到计算机名
            string strPcName = Dns.GetHostName();
            //得到本机IP地址数组
            IPHostEntry ipEntry = Dns.GetHostEntry(strPcName);
            //遍历数组
            foreach(var IPadd in ipEntry.AddressList)
            {
                //判断当前字符串是否为正确IP地址
                if (IsRightIP(IPadd.ToString())) 
                {
                    //得到本地IP地址
                    strLocalIP = IPadd.ToString();                   
                    break;
                }
            }           
            return strLocalIP;
        }
 
      //得到网关地址
        private string GetGateway() 
        {  
            string strGateway = "";
            //获取所有网卡
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            //遍历数组
            foreach (var netWork in nics)
            {
                //单个网卡的IP对象
                IPInterfaceProperties ip = netWork.GetIPProperties();
                //获取该IP对象的网关
                GatewayIPAddressInformationCollection gateways = ip.GatewayAddresses;
                foreach(var gateWay in gateways)
                {
                    //如果能够Ping通网关
                    if(IsPingIP(gateWay.Address.ToString()))
                    {
                        //得到网关地址
                        strGateway = gateWay.Address.ToString();
                        //跳出循环
                        break;
                    }
                }
                //如果已经得到网关地址
                if (strGateway.Length > 0) 
                {                
                    break;
                }
            }           
            return strGateway;
        }
        /// 尝试Ping指定IP是否能够Ping通      
        /// <param name="strIP">指定IP</param>
        /// <returns>true 是 false 否</returns>
        public static bool IsPingIP(string strIP) 
        {
            try
            {                
                Ping ping = new Ping();
                //接受Ping返回值
                PingReply reply = ping.Send(strIP, 1000);
                //Ping通
                return true;
            }
            catch 
            {
                //Ping失败
                return false;
            }
        }


                
可以使用C#中的System.Net.NetworkInformation.NetworkInterface类和System.Net.NetworkInformation.IPAddressCollection类来设置本机IP地址/子网/网关/DNS。 首先需要获取本机的网络接口,可以通过以下代码实现: ``` NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); ``` 然后可以遍历接口列表,找到需要设置的接口。假设我们要设置的是第一个接口,可以通过以下代码获取该接口的IP配置信息: ``` IPInterfaceProperties ipProperties = interfaces[0].GetIPProperties(); ``` 接下来,可以通过以下代码来设置IP地址/子网/网关: ``` UnicastIPAddressInformation ipInfo = ipProperties.UnicastAddresses[0]; IPAddress ipAddress = ipInfo.Address; IPAddress subnetMask = ipInfo.IPv4Mask; GatewayIPAddressInformation gatewayInfo = ipProperties.GatewayAddresses[0]; IPAddress gatewayAddress = gatewayInfo.Address; ``` 最后,可以通过以下代码来设置DNS服务器地址: ``` ipProperties.DnsAddresses.Clear(); ipProperties.DnsAddresses.Add(IPAddress.Parse("8.8.8.8")); ``` 完整代码如下: ``` NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); IPInterfaceProperties ipProperties = interfaces[0].GetIPProperties(); UnicastIPAddressInformation ipInfo = ipProperties.UnicastAddresses[0]; IPAddress ipAddress = ipInfo.Address; IPAddress subnetMask = ipInfo.IPv4Mask; GatewayIPAddressInformation gatewayInfo = ipProperties.GatewayAddresses[0]; IPAddress gatewayAddress = gatewayInfo.Address; ipProperties.DnsAddresses.Clear(); ipProperties.DnsAddresses.Add(IPAddress.Parse("8.8.8.8")); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值