继续发C#获取内网IP地址的代码(原创的)
Page 1 of 1
继续发C#获取内网IP地址的代码(原创的)
获取内网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;
}
}
}
刚才有人回复说看不懂,这次代码里卖弄我给出注释吧。
以下为代码部分:
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
- Posts : 211
Points : 72995
Reputation : 0
Join date : 2014-06-18
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|