zoukankan      html  css  js  c++  java
  • .net中c#获取本机IP地址实例代码

    * 在使用前,一定要注意在头部加上引用:

    using System.Net;

    代码如下:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Net;
     5 using System.Text;
     6 
     7 namespace ConsoleApplication1
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Console.Write(new Program().GetHostInfo());
    14             Console.ReadLine();
    15         }
    16         //获取本地IP等信息
    17         protected string GetHostInfo()
    18         {
    19             StringBuilder Info = new StringBuilder("");
    20             IPAddress[] ipHost = Dns.GetHostAddresses(Dns.GetHostName());
    21             Info.Append("本机名:");
    22             Info.Append(Dns.GetHostName());
    23             Info.Append(" -> ");
    24             Info.Append("IP 地址:");
    25             Info.Append(" -> ");
    26             foreach (IPAddress address in ipHost)
    27             {
    28                 Info.Append(address.ToString());
    29                 Info.Append(" >> ");
    30             }
    31             return Info.ToString();
    32         }
    33     }
    34 }

     * 但是以上代码还不能满足 程序员的要求,想要的也许就是一个192.168.1.66这样的IP,所以需要进行过滤筛选,代码如下:

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Net;
      5 using System.Net.NetworkInformation;
      6 using System.Text;
      7 using System.Text.RegularExpressions;
      8 
      9 namespace ConsoleApplication1
     10 {
     11     class Program
     12     {
     13         static void Main(string[] args)
     14         {
     15             String sd = new Program().GetLocalIP();
     16             Console.WriteLine(sd);
     17             Console.ReadLine();
     18         }
     19         /// <summary>
     20         /// 获取本地IP等信息
     21         /// </summary>
     22         /// <returns></returns>
     23         private string GetLocalIP()
     24         {
     25             //本机IP地址 
     26             string strLocalIP = "";
     27             //得到计算机名 
     28             string strPcName = Dns.GetHostName();
     29             //得到本机IP地址数组 
     30             IPHostEntry ipEntry = Dns.GetHostEntry(strPcName);
     31             //遍历数组 
     32             foreach (var IPadd in ipEntry.AddressList)
     33             {
     34                 //判断当前字符串是否为正确IP地址 
     35                 if (IsRightIP(IPadd.ToString()))
     36                 {
     37                     //得到本地IP地址 
     38                     strLocalIP = IPadd.ToString();
     39                     //结束循环 
     40                     break;
     41                 }
     42             }
     43 
     44             //返回本地IP地址 
     45             return strLocalIP;
     46         }
     47         /// <summary>
     48         /// 判断是否为正确的IP地址 
     49         /// </summary>
     50         /// <param name="strIPadd">IP</param>
     51         /// <returns></returns>
     52         public static bool IsRightIP(string strIPadd)
     53         {
     54             //利用正则表达式判断字符串是否符合IPv4格式 
     55             if (Regex.IsMatch(strIPadd, @"^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$"))
     56             {
     57                 //根据小数点分拆字符串 
     58                 string[] ips = strIPadd.Split('.');
     59                 if (ips.Length == 4 || ips.Length == 6)
     60                 {
     61                     //如果符合IPv4规则 
     62                     if (System.Int32.Parse(ips[0]) < 256 && System.Int32.Parse(ips[1]) < 256 & System.Int32.Parse(ips[2]) < 256 & System.Int32.Parse(ips[3]) < 256)
     63                     {
     64                         if(IsPingIP(strIPadd))
     65                             //正确 
     66                             return true;
     67                         else
     68                             //错误 
     69                             return false;
     70                     }
     71                         
     72                     //如果不符合 
     73                     else
     74                         //错误 
     75                         return false;
     76                 }
     77                 else
     78                     //错误 
     79                     return false;
     80             }
     81             else
     82                 //错误 
     83                 return false;
     84         }
     85         /// <summary>
     86         /// 尝试Ping指定IP 是否能够Ping通 
     87         /// </summary>
     88         /// <param name="strIP">IP</param>
     89         /// <returns></returns>
     90         public static bool IsPingIP(string strIP)
     91         {
     92             try
     93             {
     94                 //创建Ping对象 
     95                 Ping ping = new Ping();
     96                 //接受Ping返回值 
     97                 PingReply reply = ping.Send(strIP, 1000);
     98                 //Ping通 
     99                 return true;
    100             }
    101             catch
    102             {
    103                 //Ping失败 
    104                 return false;
    105             }
    106         }
    107     }
    108 }
    点我看代码~

     * 参考:http://www.111cn.net/net/160/47427.htm

  • 相关阅读:
    【Cocos2dx 3.3 Lua】触屏事件
    (转载)【cocos2dx 3.x Lua] 注册事件函数详解
    【Cocos2dx 3.x Lua】CCParallaxNode使用
    TileMap地图
    linux rdate
    sqlmap 技巧整理
    linux ckconfig
    ubuntu remove mysql
    aliyun ubuntu mysql
    解决mysql“Access denied for user 'root'@'localhost'”
  • 原文地址:https://www.cnblogs.com/willingtolove/p/4607381.html
Copyright © 2011-2022 走看看