Get PC Physical Address
namespaces: Using System.Management;
Copy Code code as follows:
<summary>
Get Mac Physical 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 "";
}
}
Get flat MAC Address:
Namespaces:
Using System.Net;
Using System.Runtime.InteropServices;
To add a variable:
Copy Code code 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 Code code 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 networked")
{
int iretn = COMMONWS. GetInt ("crrt/pad-login", "Specify a flat MAC address for a patient to be treated", 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 of destination
Int32 lhost = inet_addr (""); IP for 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;
}