C#中如何得到主机名与IP地址

来源:岁月联盟 编辑:zhu 时间:2006-04-07
 

/*
 * Copyright (c) 2006,四川师范大学
 * All rights reserved.
 * 文件名称:GetIpAndName
 * 文件标识:见配置管理计划书
 * 文件摘要:得到本地主机的名字与IP
 */


using System;
using System.Net;


/*
 * 当前版本:1.0
 * 软件作者:安美洪
 * 完成日期:2006年3月28日
 *
 * 取代版本:无
 * 原作者  :无
 * 完成日期:无
 */


namespace GetIpAndName
{

 class Class1
 {
 
  [STAThread]
  static void Main(string[] args)
  {
   //得到主机名
   string name = Dns.GetHostName();
   Console.WriteLine("主机名字:{0}",name);

   IPHostEntry me = Dns.GetHostByName(name);

   //输出得到的IP
   foreach (IPAddress ip in me.AddressList)
   {
    Console.WriteLine("IP  地址:{0}",ip.ToString());
   }
   Console.Read();
  
  }
 }
}