当前位置:编程学习 > C#/ASP.NET >>

C#根据域名查询IP(CMD命令参数输入或者启动程序后再输入查询)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace IPSearch
{
    class Program
    {
        static void Main(string[] args)
        {
            string www = "";
            if (args.Length > 0)
            {
                www = args[0];
                if (string.IsNullOrEmpty(www) || string.IsNullOrWhiteSpace(www))
                {
                    Console.WriteLine("Input is empty!");
                    www = inputWWW();
                }
            }
            else
            {
                www = inputWWW();
            }
            Console.WriteLine("Search...");
            while (true)
            {                             
                IPAddress[] ipAddresses = null;
                try
                {
                    ipAddresses = Dns.GetHostAddresses(formatWWW(www));//Important.
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    www = inputWWW();
                    continue;
                }
                foreach (IPAddress ipAddress in ipAddresses)
                {                    
                    Console.WriteLine(ipAddress.ToString());
                }
                Console.WriteLine("Search Completed!");
                Console.WriteLine("Q:Quit.Other key:Continue.");
                www = Console.ReadLine();
                if (www.ToUpper().Equals("Q"))
                {
                    break;
                }
                else
                {
                    www = inputWWW();
                }                
            }           
        }

        private static string inputWWW()
        {
            Console.WriteLine("Input www");
            string www = Console.ReadLine();
            if (string.IsNullOrEmpty(www) || string.IsNullOrWhiteSpace(www))
            {
                Console.WriteLine("Input is empty!If you want to exit,please input Q!");
                www = Console.ReadLine();
                if (www.ToUpper().Equals("Q"))
                {
                    Environment.Exit(1);
                }
                www = inputWWW();               
            }  
            return www;
        }

        private static string formatWWW(string www)
        {
            if (string.IsNullOrEmpty(www) || string.IsNullOrWhiteSpace(www))
            {
                return "";
            }
            string newWWW = www.ToUpper().Replace("HTTP://", "");
            newWWW = newWWW.Split('/')[0];
            return newWWW;
        }
    }
}

 

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,