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

有时因为需要,希望知道域名的IP,那用C#怎么实现呢?以下是实现代码

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


使用方式如下:(假定上面的代码编译的程序名为IPSearch.exe)

1.使用命令参数查询:打开CMD命令后,输入程序路径,再输入空格,然后输入域名查询。具体参见下图红色部分。


如果不想输入路径,可以将IPSearch.exe的路径配置成环境变量。

WIN7中配置方法为:

我的电脑->右键->属性->高级系统设置->环境变量->双击系统变量下的Path->填入IPSearch.exe的路径(与前面的路径需用分号隔开)->确定(编辑系统变量)->确定(环境变量)->确定(系统属性)

具体可参见下图红色部分,自左向右操作。



设置好环境变量后,就可以不输入路径,直接查询。见下图。

2.启动程序后直接查询,参见下图


转载请注明出处http://blog.csdn.net/xxdddail/article/details/9379719

原文地址:https://www.cnblogs.com/dyllove98/p/3201325.html