日期:2011-10-03  浏览次数:20413 次

.aspx代码

<form id="Form1" method="post" runat="server">
   <FONT face="宋体">
    <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 256px; POSITION: absolute; TOP: 96px" runat="server"
     Width="248px"></asp:TextBox>
    <asp:Button id="Button2" style="Z-INDEX: 105; LEFT: 352px; POSITION: absolute; TOP: 128px" runat="server"
     Text="转化为域名"></asp:Button>
    <asp:Label id="Label2" style="Z-INDEX: 104; LEFT: 256px; POSITION: absolute; TOP: 176px" runat="server"></asp:Label>
    <asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 256px; POSITION: absolute; TOP: 128px" runat="server"
     Text="转化为IP"></asp:Button>
    <asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 256px; POSITION: absolute; TOP: 64px" runat="server">通过域名获得IP地址</asp:Label></FONT>
  </form>

.aspx.cs代码

using System.Net;

private void Button1_Click(object sender, System.EventArgs e)
  {//转化为IP地址
   IPHostEntry hostInfo=Dns.GetHostByName(TextBox1.Text);
   Label2.Text=hostInfo.AddressList[0].ToString();
  }

  private void Button2_Click(object sender, System.EventArgs e)
  {//转化为域名
   IPHostEntry hostInfo=Dns.GetHostByAddress(TextBox1.Text);
   Label2.Text=hostInfo.HostName;
  }

获得主机名和IP地址

private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   Dns dns;
   string hostname=Dns.GetHostName();
   IPAddress[] ip=Dns.Resolve(hostname).AddressList;
   Label2.Text="机器名称:"+hostname.ToString()+"<br>IP地址:"+ip[0].ToString();
  }