c# 获取本机IP 实用代码


 1 /// <summary>
 2         /// 获取本机IP
 3         /// </summary>
 4         /// <param name="complete">true 为完整IP false IP段</param>
 5         /// <returns></returns>
 6         public static string GetIP(bool complete)
 7         {
 8             try
 9             {
10                 IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
11                 foreach (IPAddress item in IpEntry.AddressList)
12                 {
13                     //AddressFamily.InterNetwork  ipv4
14                     //AddressFamily.InterNetworkV6 ipv6
15                     if (item.AddressFamily == AddressFamily.InterNetwork)
16                     {
17                         if (complete == true)
18                         {
19                             return item.ToString();
20                         }
21                         else
22                         {
23                             string[] iparr2 = item.ToString().Split('.');
24                             if (iparr2.Length == 4)
25                             {
26                                 return iparr2[0] + "." + iparr2[1] + "." + iparr2[2] + ".";
27                             }
28                         }
29                     }
30                 }
31                 return "";
32             }
33             catch { return ""; }
34         }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM