熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP.NET獲取IP與MAC地址的方法

2022-06-13   來源: .NET編程 
    獲取服務器的IP地址方法以DNS法較為簡單實用如下

private void ButtonIP_Click(object sender SystemEventArgs e)

{ Syste 

mNetIPAddress[] addressList = DnsGetHostByName(DnsGetHostName())AddressList;

if ( addressListLength>)

{ TextLIPText = addressList[]ToString();

TextSIPText = addressList[]ToString();

}

else

{

TextLIPText = addressList[]ToString();

TextSIPText = 沒有可用的連接;

}

}

    獲取服務器的IP地址與MAC地址另一方法如下

using SystemManagement;

string stringMAC = ;

string stringIP = ;

ManagementClass MC = new ManagementClass Win_NetworkAdapterConfiguration);

ManagementObjectCollection MOC= MCGetInstances();

foreach(ManagementObject MO in MOC)

{

if ((bool)MO[IPEnabled] == true)

{

stringMAC += MO[MACAddress]ToString();

TextMACText = stringMACToString();

string[] IPAddresses = (string[]) MO[IPAddress];

if(IPAddressesLength > )

stringIP = IPAddresses[];

TextIPText = stringIPToString();

}

}

    獲取客戶端本機的IP地址相當簡易方法如下

using SystemNet;

TextIPText=PageRequestUserHostAddress;

    如要獲取客戶端本機的MAC地址就相對復雜些得導入調用兩個API用ARP協議獲取但這樣只能獲取到同網段機器的MAC對於跨網段的得利用IP掃描或cmd中nbtstat命令獲取MAC地址也可通過讀取系統注冊表值或WMI的數據庫來獲取各位如有簡易可行的好辦法請留言告訴我


From:http://tw.wingwit.com/Article/program/net/201311/15096.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.