直接上一段用于内网获取本机ip地址的方法,更全面的可以先获取本机网络类型,在获取ip

 /// <summary>
        /// 获取本地连接ip
        /// </summary>
        /// <returns></returns>
        private string getLocalIp()
        {
            NetworkInterface[] interfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
            int len = interfaces.Length;
            string localIP = "未找到";
            for (int i = 0; i < len; i++)
            {
                NetworkInterface ni = interfaces[i];
                if (ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet || ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                {
                    if (ni.Name == "本地连接" || ni.Name=="无线网络连接")
                    {
                        IPInterfaceProperties property = ni.GetIPProperties();
                        foreach (UnicastIPAddressInformation ip in   property.UnicastAddresses)
                        {
                            if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                            {
                               localIP= ip.Address.ToString();
                            }
                        }
                    }
                }

            }
            return localIP;
        }

猜你喜欢

转载自blog.csdn.net/zhongheijituan/article/details/39235961