[英]How do I get the Local Network IP address of a computer programmatically?
[英]How can I get computer name and IP address of local computer
如何以編程方式獲取PC的計算機名稱和IP地址? 例如,我想在文本框中顯示該信息。
查看更多相關信息: 如何獲取機器的IP地址
System.Security.Principal.WindowsPrincipal p = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal;
string strName = p.Identity.Name;
To get the machine name,
System.Environment.MachineName
or
using System.Net;
strHostName = DNS.GetHostName ();
// Then using host name, get the IP address list..
IPHostEntry ipEntry = DNS.GetHostByName (strHostName);
IPAddress [] addr = ipEntry.AddressList;
for (int i = 0; i < addr.Length; i++)
{
Console.WriteLine ("IP Address {0}: {1} ", i, addr[i].ToString ());
}
簡單的方式..
string IP_Address = Dns.GetHostByName(Environment.MachineName).AddressList[0].toString();
我使用以下代碼: https : //stackoverflow.com/a/27376368/2510099獲取IP地址
public string GetIPAddress()
{
string ipAddress = null;
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
{
socket.Connect("8.8.8.8", 65530); //Google public DNS and port
IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
ipAddress = endPoint.Address.ToString();
}
return ipAddress;
}
並為機器名稱
Environment.MachineName;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.