首先使用C# 中的Dns.GetHostName() 方法尋找主機名稱-
String hostName = string.Empty; hostName = Dns.GetHostName(); Console.WriteLine("Hostname: "+hostName);
現在,使用IPHostEntry.AddressList 屬性來取得IP 位址-
IPHostEntry myIP = Dns.GetHostEntry(hostName); IPAddress[] address = myIP.AddressList;
嘗試以下程式碼來顯示IP 位址-
using System; using System.Net; class Program { static void Main() { String hostName = string.Empty; hostName = Dns.GetHostName(); IPHostEntry myIP = Dns.GetHostEntry(hostName); IPAddress[] address = myIP.AddressList; for (int i = 0; i < address.Length; i++) { Console.WriteLine("IP Address {1} : ",address[i].ToString()); } Console.ReadLine(); } }
以上是C# 程式尋找客戶端的 IP 位址的詳細內容。更多資訊請關注PHP中文網其他相關文章!