c# - 如何在 c# windows 8 Metro App 中从主机名获取 IP 地址?

标签 c# windows-8

<分区>

我正在将 Windows 窗体类库迁移到 Metro 应用程序类库。那里有一个代码块,它根据主机名给出 IP 地址,如下所示,

IPHostEntry ipHostInfo = Dns.GetHostEntry(Address);   
IPAddress ipAddress = ipHostInfo.AddressList[0];// IPAddress.Parse(address);
IPEndPoint endPoint = new IPEndPoint(ipAddress, Port);

例如:

地址:talk.google.com

ip地址:xx.xxx.xxx.xx

但我看到 Metro App System.Net 中没有 IPHostEntryDnsIPAddress。 .

如果有人知道,请告诉我这些在 Windows 8 Metro 应用程序中的替代品。

最佳答案

using System.Threading.Tasks;

public async static Task<string> ResolveDNS(string remoteHostName)
    {
        if (string.IsNullOrEmpty(remoteHostName))
            return string.Empty;

        string ipAddress = string.Empty;

        try
        {
            IReadOnlyList<EndpointPair> data =
              await DatagramSocket.GetEndpointPairsAsync(new HostName(remoteHostName), "0");

            if (data != null && data.Count > 0)
            {
                foreach (EndpointPair item in data)
                {
                    if (item != null && item.RemoteHostName != null &&
                                  item.RemoteHostName.Type == HostNameType.Ipv4)
                    {
                        return item.RemoteHostName.CanonicalName;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            ipAddress = ex.Message;
        }

        return ipAddress;
    } 

关于c# - 如何在 c# windows 8 Metro App 中从主机名获取 IP 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13489832/

相关文章:

c# - C# 中的数组属性语法

c# - 适用于 Windows 8 的 spy 实用程序

.net - 可移植类库和.NET ConcurrentDictionary

c# - 关闭 Windows 8 Charm Bar

c# - Windows 8 应用程序 : accessing local and network files

windows-8 - Metro/WinRT UI 异步无效事件处理程序未调用未处理的异常处理程序

c# - 将 JSON 字符串中的数据覆盖到现有对象实例

c# - 使用 .NET API 保护和加密 ActiveMQ 消息

c# - 从 Select Bigquery 创建表

c# - 我应该为此使用线程吗?