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;
}