Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在WP 8.1中获取设备IP?_C#_Windows 8_Windows Phone 8.1_Win Universal App - Fatal编程技术网

C# 如何在WP 8.1中获取设备IP?

C# 如何在WP 8.1中获取设备IP?,c#,windows-8,windows-phone-8.1,win-universal-app,C#,Windows 8,Windows Phone 8.1,Win Universal App,我只需要获取本地网络中设备的当前IP。 类似于 var IP = IPInformation.GetIP(); 很抱歉这个简单的问题。。。只是找不到什么。IPAddresses方法获取所选服务器IP地址信息。 然后,它将显示服务器支持的地址族类型及其名称 标准和字节格式的IP地址 private static void IPAddresses(string server) { try { System.Text.ASCIIEncoding

我只需要获取本地网络中设备的当前IP。 类似于

var IP = IPInformation.GetIP();

很抱歉这个简单的问题。。。只是找不到什么。

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");

        }
  }
您需要通过网络信息类Windows.Networking.Connectivity.NetworkInformation使用GetHostNames方法

您将检索包含DisplayName属性中所有IP地址的主机名对象集合

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);
               }
        }