编程语言
首页 > 编程语言> > ASP.NET获取IP与MAC[using C#]

ASP.NET获取IP与MAC[using C#]

作者:互联网

获取服务器的IP地址方法以DNS法较为简单实用,如下:
  using System.Net;

 

 private void ButtonIP_Click(object sender, System.EventArgs e)
  { 
     System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
     if ( addressList.Length>1)
     {   TextLIP.Text = addressList[0].ToString();
          TextSIP.Text = addressList[1].ToString();
     }
     else
     {
          TextLIP.Text = addressList[0].ToString();
          TextSIP.Text = "没有可用的连接";
     }
  }

获取服务器的IP地址与MAC地址另一方法如下:

   using System.Management;

 

   string stringMAC = "";
   string stringIP = "";
   ManagementClass MC = new ManagementClass "Win32_NetworkAdapterConfiguration");
   ManagementObjectCollection MOC= MC.GetInstances();
  
   foreach(ManagementObject MO in MOC)
   {
    if ((bool)MO["IPEnabled"] == true)
    {
     stringMAC += MO["MACAddress"].ToString();
     TextMAC.Text = stringMAC.ToString();
     string[] IPAddresses = (string[]) MO["IPAddress"];
     if(IPAddresses.Length > 0)
     stringIP = IPAddresses[0];
     TextIP.Text = stringIP.ToString();
    }
   } 

 

获取客户端本机的IP地址相当简易,方法如下:

using System.Net;

 

TextIP.Text=Page.Request.UserHostAddress;

转载于:https://www.cnblogs.com/Tim_Liu/archive/2010/11/22/1883978.html

标签:ASP,string,C#,IP,System,addressList,ToString,Text,using
来源: https://blog.csdn.net/weixin_33928137/article/details/94724533