获取本机正在使用的ipv4地址(访问互联网的IP)

【转】原文地址:http://www.cnblogs.com/lijianda/p/6604651.html

1.一个电脑有多个网卡,有线的、无线的、还有vmare虚拟的两个网卡。
2.就算只有一个网卡,但是该网卡配置了N个IP地址.其中还包括ipv6地址。

 

  1 /// <summary>  
  2   /// 获取当前使用的IP  
  3   /// </summary>  
  4   /// <returns></returns>  
  5   public static string GetLocalIP()  
  6   {  
  7       string result = RunApp("route", "print",true);  
  8       Match m = Regex.Match(result, @"0.0.0.0\s+0.0.0.0\s+(\d+.\d+.\d+.\d+)\s+(\d+.\d+.\d+.\d+)");  
  9       if (m.Success)  
 10       {  
 11           return m.Groups[2].Value;  
 12       }  
 13       else  
 14       {  
 15           try  
 16           {  
 17               System.Net.Sockets.TcpClient c = new System.Net.Sockets.TcpClient();  
 18               c.Connect("www.baidu.com", 80);  
 19               string ip = ((System.Net.IPEndPoint)c.Client.LocalEndPoint).Address.ToString();  
 20               c.Close();  
 21               return ip;  
 22           }  
 23           catch (Exception)  
 24           {  
 25               return null;  
 26           }  
 27       }  
 28   }  
 29   
 30   /// <summary>  
 31   /// 获取本机主DNS  
 32   /// </summary>  
 33   /// <returns></returns>  
 34   public static string GetPrimaryDNS()  
 35   {  
 36       string result = RunApp("nslookup", "",true);  
 37       Match m = Regex.Match(result, @"\d+\.\d+\.\d+\.\d+");  
 38       if (m.Success)  
 39       {  
 40           return m.Value;  
 41       }  
 42       else  
 43       {  
 44           return null;  
 45       }  
 46   }  
 47   
 48   /// <summary>  
 49   /// 运行一个控制台程序并返回其输出参数。  
 50   /// </summary>  
 51   /// <param name="filename">程序名</param>  
 52   /// <param name="arguments">输入参数</param>  
 53   /// <returns></returns>  
 54   public static string RunApp(string filename, string arguments,bool recordLog)  
 55   {  
 56       try  
 57       {  
 58           if (recordLog)  
 59           {  
 60               Trace.WriteLine(filename + " " + arguments);  
 61           }  
 62           Process proc = new Process();  
 63           proc.StartInfo.FileName = filename;  
 64           proc.StartInfo.CreateNoWindow = true;  
 65           proc.StartInfo.Arguments = arguments;  
 66           proc.StartInfo.RedirectStandardOutput = true;  
 67           proc.StartInfo.UseShellExecute = false;  
 68           proc.Start();  
 69   
 70           using (System.IO.StreamReader sr = new System.IO.StreamReader(proc.StandardOutput.BaseStream, Encoding.Default))  
 71           {  
 72               //string txt = sr.ReadToEnd();  
 73               //sr.Close();  
 74               //if (recordLog)  
 75               //{  
 76               //    Trace.WriteLine(txt);  
 77               //}  
 78               //if (!proc.HasExited)  
 79               //{  
 80               //    proc.Kill();  
 81               //}  
 82               //上面标记的是原文,下面是我自己调试错误后自行修改的  
 83               Thread.Sleep(100);           //貌似调用系统的nslookup还未返回数据或者数据未编码完成,程序就已经跳过直接执行  
 84                                            //txt = sr.ReadToEnd()了,导致返回的数据为空,故睡眠令硬件反应  
 85               if (!proc.HasExited)         //在无参数调用nslookup后,可以继续输入命令继续操作,如果进程未停止就直接执行  
 86               {                            //txt = sr.ReadToEnd()程序就在等待输入,而且又无法输入,直接掐住无法继续运行  
 87                   proc.Kill();  
 88               }  
 89               string txt = sr.ReadToEnd();  
 90               sr.Close();  
 91               if (recordLog)  
 92                   Trace.WriteLine(txt);  
 93               return txt;  
 94           }  
 95       }  
 96       catch (Exception ex)  
 97       {  
 98           Trace.WriteLine(ex);  
 99           return ex.Message;  
100       }  
101   }

 

转载于:https://www.cnblogs.com/yougmi/p/6957895.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值