Obtain physical addresses of PCs and tablets

Source: Internet
Author: User

Obtain the physical address of a pc

Namespace: using System. Management;
Copy codeThe Code is as follows:
/// <Summary>
/// Obtain the mac address
/// </Summary>
/// <Returns> </returns>
Public string GetMac ()
{
Try
{
ManagementObjectSearcher query = new ManagementObjectSearcher ("SELECT * FROM Win32_NetworkAdapterConfiguration ");
ManagementObjectCollection queryCollection = query. Get ();
Foreach (ManagementObject mo in queryCollection)
{
If (mo ["IPEnabled"]. ToString () = "True ")
Return mo ["MacAddress"]. ToString ();
}
Return "";
}
Catch
{
Return "";
}
}

Obtain the flat mac address:

Namespace:

Using System. Net;
Using System. Runtime. InteropServices;

Add variable:
Copy codeThe Code is as follows:
Protected static string strJavaScript = string. Empty;
[DllImport ("Iphlpapi. dll")]
Private static extern int SendARP (Int32 dest, Int32 host, ref Int64 mac, ref Int32 length );
[DllImport ("Ws2_32.dll")]
Private static extern Int32 inet_addr (string ip );

Copy codeThe Code is as follows:
Private void SetMac ()
{
Try
{
String treatment_id = Hid_treatment_id.Value;
String mac = string. Empty;

String ip = Request. UserHostAddress. ToString (). Trim ();
Try
{
Mac = getMac ();
}
Catch {lbl. InnerText = "w33w ";}
// Mac = "00: 01: 36: D8: 9C: C4 ";
// Lbl. InnerText = mac + "," + ip;
If (mac! = "You are not connected to the Internet ")
{
Int iRetn = commonws. GetInt ("CRRT/PAD-Logon", "specify the tablet MAC address of a patient", new string [] {mac, treatment_id });
}
}
Catch {lbl. InnerText = "ww ";}
}

Private string getMac ()
{
String mac = "";
String strClientIP = Request. UserHostAddress. ToString (). Trim ();
Int32 ldest = inet_addr (strClientIP); // ip address of the destination
Int32 lhost = inet_addr (""); // ip address of the local server
Int64 macinfo = new Int64 ();
Int32 len = 6;
Int res = SendARP (ldest, 0, ref macinfo, ref len );
Mac = macinfo. ToString ("X ");
Try
{
String [] arr_mac = new string [mac. Length/2];
Int k = 0;
For (int I = mac. Length; I> 0; I = I-2)
{
Arr_mac [k] = mac. Substring (I-2, 2 );
K ++;
}
Mac = "";
Foreach (string chars in arr_mac)
{
Mac + = chars + ":";
}
Mac = mac. TrimEnd (':');
}
Catch {}
Return mac;

}

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.