Evo. G Tech Team Forum
Welcome to Evo. G Tech Team Forum. We have moved to a new website : www.evogtechteam.com

Thanks you.

by Evo. G Tech Team Management.

Join the forum, it's quick and easy

Evo. G Tech Team Forum
Welcome to Evo. G Tech Team Forum. We have moved to a new website : www.evogtechteam.com

Thanks you.

by Evo. G Tech Team Management.
Evo. G Tech Team Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

继续发C#获取内网IP地址的代码(原创的)

Go down

继续发C#获取内网IP地址的代码(原创的) Empty 继续发C#获取内网IP地址的代码(原创的)

Post by cyjian December 13th 2014, 09:56

获取内网IP地址实际上是很简单的,刚才写了外网的,现在把内网的也贴上来吧。
刚才有人回复说看不懂,这次代码里卖弄我给出注释吧。

以下为代码部分:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace GetIPAddress
{
    class Program
    {
        public static int Main(string[] args)
        {

            String strHostName;
            if (args.Length == 0)
            {
                // 得到本地机器的主机名称
                strHostName = Dns.GetHostName();
                Console.WriteLine("Local Machine's Host Name: " + strHostName);
            }
            else
            {
                strHostName = args[0];
            }

            // 通过主机名称得到IP地址列表
            IPHostEntry ipEntry = Dns.GetHostByName(strHostName);
            IPAddress[] addr = ipEntry.AddressList;

            //将获取到IP地址逐个输出
            for (int i = 0; i < addr.Length; i++)
            {
                Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString());
            }
            return 0;
        }
    }
}

cyjian
Spammer
Spammer

Posts : 211
Points : 72995
Reputation : 0
Join date : 2014-06-18

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum