获取本机正在使用的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   }

 

posted @ 2017-06-07 16:50  Young汨  阅读(2927)  评论(0编辑  收藏  举报