C#获取本地IP地址

C#获取本地IP地址

public static string GetLocalIp()
{
  IPAddress localIp=null;

try
{
     IPAddress[] ipArray;  
     ipArray=Dns.GetHostAddress(Dns.GetHostName());
     localIp=ipArray.Frist(ip=>ip.AddressFamily==AddressFamily.InterNetwork);
    
}  
catch(Exception ex)
{
}  
if(localIp==null)
{
   localIp=IPAddress.Parse("127.0.0.1");
}
return localIp.ToString();
}

 

posted @ 2016-06-14 17:15  想念熊小米  阅读(5107)  评论(2编辑  收藏  举报