利用C#根据域名查询其绑定的服务器IP,在之前介绍的做网站里面有介绍到IP和域名的知识。看运行效果截图

C#根据域名查IP

上代码,这个显然是控制台的哈,大家复制以下代码直接黏贴就可以用了哦,本站原创哈luyugao.com转载请保留本文链接,谢谢

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;//网络协议
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "查询域名绑定的IP地址";//控制台窗口标题
            do
            {
                Console.ForegroundColor = ConsoleColor.White;//文字颜色
                Console.Write("请输入域名(如:www.baidu.com),输入0退出:");
                string domain = Console.ReadLine();//获取用户输入的域名
                if (domain != "0")
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("输入的域名是:" + domain);
                    Console.ForegroundColor = ConsoleColor.Red;
                    try
                    {
                        IPAddress[] ips = Dns.GetHostAddresses(domain);
                        foreach (IPAddress ip in ips)
                            Console.WriteLine(domain + ":" + ip.ToString());
                    }
                    catch { Console.WriteLine(domain + " 查询失败"); }
                }
                else
                    break;//跳出循环 
            } while (true);
        }
    }
}