---
title: "C#获取本地IP"
author: "devilhand"
date: "2012-06-06T14:39:03Z"
tags: []
categories: ["编程语言"]
slug: "c-ip"
draft: false
lastmod: "2012-06-06T14:39:03Z"
id: 1553265
url: "https://devilhand.iteye.com/blog/1553265"
---

```
        /// <summary>
        /// 获取本地Ip
        /// </summary>
        private string GetLocalIP()
        {
            IPHostEntry ipHostEntry = Dns.GetHostEntry(Dns.GetHostName());
            if (ipHostEntry.AddressList.Length > 0)
            {
                foreach (IPAddress ipAddress in ipHostEntry.AddressList)
                {
                    if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
                    {
                        return ipAddress.ToString();
                    }
                }
            }
            return string.Empty;
        }
```