我只需要在本地网络中获取设备的当前IP。 有点像...
var IP = IPInformation.GetIP();
很抱歉这个简单的问题......找不到东西。
答案 0 :(得分:2)
您需要通过GetHostNames()
类(NetworkInformation
)使用Windows.Networking.Connectivity.NetworkInformation
方法。
您将检索包含所有IP地址(在DisplayName
属性中)
List<string> ipAddresses = new List<string>();
var hostnames = NetworkInformation.GetHostNames();
foreach (var hn in hostnames)
{
//IanaInterfaceType == 71 => Wifi
//IanaInterfaceType == 6 => Ethernet (Emulator)
if (hn.IPInformation != null &&
(hn.IPInformation.NetworkAdapter.IanaInterfaceType == 71
|| hn.IPInformation.NetworkAdapter.IanaInterfaceType == 6))
{
string ipAddress = hn.DisplayName;
ipAddresses.Add(ipAddress);
}
}
答案 1 :(得分:1)
IPAddresses方法获取所选的服务器IP地址信息。 然后显示服务器支持的地址系列类型及其类型 标准和字节格式的IP地址。
private static void IPAddresses(string server)
{
try
{
System.Text.ASCIIEncoding ASCII = new System.Text.ASCIIEncoding();
// Get server related information.
IPHostEntry heserver = Dns.GetHostEntry(server);
// Loop on the AddressList
foreach (IPAddress curAdd in heserver.AddressList)
{
// Display the type of address family supported by the server. If the
// server is IPv6-enabled this value is: InternNetworkV6. If the server
// is also IPv4-enabled there will be an additional value of InterNetwork.
Console.WriteLine("AddressFamily: " + curAdd.AddressFamily.ToString());
// Display the ScopeId property in case of IPV6 addresses.
if(curAdd.AddressFamily.ToString() == ProtocolFamily.InterNetworkV6.ToString())
Console.WriteLine("Scope Id: " + curAdd.ScopeId.ToString());
// Display the server IP address in the standard format. In
// IPv4 the format will be dotted-quad notation, in IPv6 it will be
// in in colon-hexadecimal notation.
Console.WriteLine("Address: " + curAdd.ToString());
// Display the server IP address in byte format.
Console.Write("AddressBytes: ");
Byte[] bytes = curAdd.GetAddressBytes();
for (int i = 0; i < bytes.Length; i++)
{
Console.Write(bytes[i]);
}
Console.WriteLine("\r\n");
}
}