簡體   English   中英

如何獲取本地計算機的計算機名稱和IP地址

[英]How can I get computer name and IP address of local computer

如何以編程方式獲取PC的計算機名稱和IP地址? 例如,我想在文本框中顯示該信息。

看看這個: 鏈接

而這: 鏈接

textBox1.Text = "Computer Name: " + Environment.MachineName

textBox2.Text = "IP Add: " + Dns.GetHostAddresses(Environment.MachineName)[0].ToString();

查看更多相關信息: 如何獲取機器的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM