可以在 Bing 查找中输入: "my IP address",然后确认这样可以看到自己的 IP。
如果需要通过代码编程来获取本机 IP,可以使用以下代码:
注意:此段代码在 WP7.1 上无法编译通过,NetworkInformation 未定义。
1 public class GetHostIPAddress 2 { 3 public string GetIPAddress() 4 { 5 string strIPAddress = null; 6 List<string> arrayIPAddress = new List<string>(); 7 // Windows.Networking.Connectivity. 8 var hostNames = Windows.Networking.Connectivity.NetworkInformation.GetHostNames(); 9 foreach (var hn in hostNames) 10 { 11 if (hn.IPInformation != null) 12 { 13 string ipAddress = hn.DisplayName; 14 arrayIPAddress.Add(ipAddress); 15 } 16 } 17 if (arrayIPAddress.Count < 1) 18 { 19 return null; 20 } 21 if (arrayIPAddress.Count == 1) 22 { 23 strIPAddress = arrayIPAddress[0]; 24 } 25 if (arrayIPAddress.Count > 1) 26 { 27 strIPAddress = arrayIPAddress[arrayIPAddress.Count - 1]; 28 } 29 // System.Console.WriteLine(); 30 for (int i = 0; i < arrayIPAddress.Count;i++ ) 31 { 32 System.Diagnostics.Debug.WriteLine("No.{0} host IP is: {1}",i + 1,arrayIPAddress[i]); 33 } 34 return strIPAddress; 35 } 36 }
调用示例:
1 { 2 GetHostIPAddress hostIP = new GetHostIPAddress(); 3 hostIP.GetIPAddress(); 4 }
运行输入如下所示:
No.1 host IP is: 169.254.135.85 No.2 host IP is: 169.254.162.98 No.3 host IP is: 192.168.0.101
时间: 2024-12-21 22:20:04