一个简单的C# DNS客户端代码

using System;
using System.Net;

class Program {
    static void Main(string[] args) {
        string hostname = "example.com";
        IPAddress[] ipAddresses = Dns.GetHostAddresses(hostname);
        if (ipAddresses == null || ipAddresses.Length == 0) {
            Console.WriteLine("Failed to resolve the hostname.");
            return;
        }

        foreach (IPAddress ip in ipAddresses) {
            Console.WriteLine(ip.ToString());
        }
    }
}

该代码首先指定要查询的主机名,然后使用Dns类的GetHostAddresses方法获取该主机名对应的所有IP地址。如果获取失败,则输出错误信息。否则,遍历所有IP地址并输出到控制台。

你可能感兴趣的:(C#,c#)