Obtain the server'sIPAddress methodDNSThe method is simple and practical, as follows:
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 =" No available connections ";
}
}
Obtain the server'sIPAddress andMacAnother address method is as follows:
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 ();
}
}
Obtain the localIPThe address is quite simple. The method is as follows:
Using system. net;
Textip. Text = page. Request. userhostaddress;
To obtain the local Mac The address is more complex. You have to import and call two API , Use ARP Obtain the protocol, but only Mac For the profit of cross-network segments IP Scan or CMD Medium NBTSTAT Command to obtain Mac Address. You can also read the system registry value or WMI . If you have a simple and feasible solution, please leave a message to me.