在控制台应用程序获取IP地址 [英] Get IP address in a console application

查看:216
本文介绍了在控制台应用程序获取IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待找出我的IP地址是从控制台应用程序是什么。

I am looking to figure out what my IP address is from a console application.

我使用 Request.ServerVariables 收集和/或 Request.UserHostAddress 。

如何能在这样一个控制台应用程序来完成?

How can this be done in a console app?

推荐答案

要做到这一点如下:最简单的方法:

The easiest way to do this is as follows:

using System;
using System.Net;


namespace ConsoleTest
{
    class Program
    {
        static void Main()
        {
            String strHostName = string.Empty;
            // Getting Ip address of local machine...
            // First get the host name of local machine.
            strHostName = Dns.GetHostName();
            Console.WriteLine("Local Machine's Host Name: " + strHostName);
            // Then using host name, get the IP address list..
            IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
            IPAddress[] addr = ipEntry.AddressList;

            for (int i = 0; i < addr.Length; i++)
            {
                Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString());
            }
            Console.ReadLine();
        }
    }
}

这篇关于在控制台应用程序获取IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆