c# 获取本机ip

 /// <summary>
        /// 取本机主机ip
        /// </summary>
        /// <returns></returns>
        public static string GetLocalIP()
        {
            try
            {
                string AddressIP = "";
                IPAddress[] ips = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
                foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
                {
                    if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
                    {
                        AddressIP = _IPAddress.ToString();
                    }
                }
                return AddressIP;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

  

posted on 2021-07-22 10:09  七七2020  阅读(54)  评论(0编辑  收藏  举报

导航