C#------如何獲取本機IP地址


轉載:

http://www.narkii.com/club/thread-396944-1.html

 

代碼:

添加引用

using System.Net.Sockets;
using System.Net;
private string GetIpAddress()
{
  string hostName = Dns.GetHostName();   //獲取本機名
  IPHostEntry localhost = Dns.GetHostByName(hostName);    //方法已過期,可以獲取IPv4的地址
  //IPHostEntry localhost = Dns.GetHostEntry(hostName);   //獲取IPv6地址
  IPAddress localaddr = localhost.AddressList[0];

  return localaddr.ToString();
}

 


免責聲明!

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



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