c#獲取本機內網和外網IP


 1 //獲取內網IP
 2 private string GetInternalIP()
 3 {
 4     IPHostEntry host;
 5     string localIP = "?";
 6     host = Dns.GetHostEntry(Dns.GetHostName());
 7     foreach (IPAddress ip in host.AddressList)
 8     {
 9         if (ip.AddressFamily.ToString() == "InterNetwork")
10         {
11             localIP = ip.ToString();
12             break;
13         }
14     }
15     return localIP;
16 }
 1 //獲取外網IP
 2 private string GetExternalIP()
 3 {
 4     string direction = "";
 5     WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
 6     using (WebResponse response = request.GetResponse())
 7     using (StreamReader stream = new StreamReader(response.GetResponseStream()))
 8     {
 9         direction = stream.ReadToEnd();
10     }
11     int first = direction.IndexOf("Address:") + 9;
12     int last = direction.LastIndexOf("</body>");
13     direction = direction.Substring(first, last - first);
14     return direction;
15 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM