2010-04-06 89 views
1

我只想得到系統的IP地址並在UI.Can有人顯示它,請告訴API用於同一如何從Net代碼的IP地址

+0

Duplicate - http://stackoverflow.com/questions/1069103/how-to-get-my-own-ip-address-in-c – 2010-04-06 09:49:50

回答

3
internal IPAddress[] GetIPAddresses() 
{ 
    string hostName = System.Net.Dns.GetHostName(); 
    IPHostEntry ihe = System.Net.Dns.GetHostEntry(hostName); 
    return ihe.AddressList; 
} 
+0

謝謝man .thats我正在尋找的 – Ravisha 2010-04-06 12:22:52

7
var address = Dns.GetHostAddresses(Dns.GetHostName()) 
       .FirstOrDefault(addr => !IPAddress.IsLoopback(addr)); 
Console.WriteLine(address); 

(這段代碼不包括本地地址127.0.0.1)

+0

+1對於linq版本 – 2010-04-06 10:03:36

+0

您可能想要更改。首先爲.FirstOrDefault,如果計算機沒有IP。 – KristoferA 2010-04-06 10:24:14

+0

@Kristofer,是的,好點,我修好了。謝謝 ! – 2010-04-06 10:26:35