C # Gets the MAC address native or remote

Source: Internet
Author: User
Tags get ip servervariables

Get the native Nic MAC address code
</summary>
<returns></returns>
<summary>
public string Getnetworkadpaterid ()
{
Try
{
String mac = "";
System.Management.ManagementClass mc = new System.Management.ManagementClass ("Win32_NetworkAdapterConfiguration");
System.Management.ManagementObjectCollection MOC = MC. GetInstances ();
foreach (System.Management.ManagementObject mo in MOC)
if ((bool) mo["ipenabled"] = = True)
{
Mac + = mo["MacAddress"]. ToString () + "";
Break
}
MOC = null;
MC = null;
Return Mac. Trim ();
}
catch (Exception e)
{
return "Umnik";
}
}

Method of obtaining MAC address based on IP address

Here's a way to get a remote MAC address
[DllImport ("Iphlpapi.dll")]
static extern int Sendarp (Int32 destip, Int32 Srcip, ref Int64 MACADDR, ref Int32 Phyaddrlen);
[DllImport ("Ws2_32.dll")]
static extern Int32 inet_addr (string ipaddr);
<summary>
Sendarp Get MAC Address
</summary>
<param name= "REMOTEIP" > Target machine IP address (192.168.1.1) </param>
<returns> MAC address of Target machine </returns>
public static string Getmacaddr_remote (String remoteip)
{
StringBuilder macAddress = new StringBuilder ();
Try
{
Int32 remote = inet_addr (REMOTEIP);
Int64 macinfo = new Int64 ();
Int32 length = 6;
Sendarp (remote, 0, ref macinfo, ref length);
String temp = convert.tostring (Macinfo, 16). PadLeft (12, ' 0 '). ToUpper ();
int x = 12;
for (int i = 0; i < 6; i++)
{
if (i = = 5)
{
Macaddress.append (temp. Substring (X-2, 2));
}
Else
{
Macaddress.append (temp. Substring (X-2, 2) + "-");
}
X-= 2;
}
return macaddress.tostring ();
}
Catch
{
return macaddress.tostring ();
}
}

Get an IP address that penetrates the proxy

Get IP Address
String user_ip = "";
if (request.servervariables["Http_via"]! = NULL)//If there is an agent then take the value directly over the proxy
{
User_ip = request.servervariables["Http_x_forwarded_for"]. ToString ();
}
Else
{
User_ip = request.servervariables["REMOTE_ADDR"]. ToString ();
}

C # Gets the MAC address native or remote

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.