C#获取局域网内在使用的IP地址

C#获取局域网内在使用的IP地址

using System.Net;
using System.Net.NetworkInformation;
       /// <summary>
        /// 获取局域网内在使用的IP地址
        /// </summary>
        /// <returns></returns>
        private string[] GetLocalIP()
        {
           string[] validIPlist = null;
           string iplist = null;
           //本机IP地址
           IPAddress ipaddress = new IPAddress(Dns.GetHostByName (Dns .GetHostName ()).AddressList [0].Address );
           string IPhead = ipaddress.ToString().Remove(ipaddress.ToString().LastIndexOf('.'));
           string IPtail = string.Empty;
           Ping ping = new Ping();
           for (int i = 0; i < 255; i++)
           {
               IPtail =i.ToString ();
               PingReply pingrep=ping .Send (IPhead +IPtail );
               if (pingrep.Status != IPStatus.BadHeader &&pingrep .Status !=IPStatus.BadRoute )
               {
                   iplist += IPhead + IPtail+" ";
               }
           }
           validIPlist = iplist.Split(' ');
           ping = null;
           return validIPlist;  
    }
posted @ 2011-11-13 17:51  @#¥%……  阅读(417)  评论(0编辑  收藏  举报