获取本机MAC和获取客户端MAC
来源:互联网 发布:遇见淘宝店 编辑:程序博客网 时间:2024/06/01 09:19
-
-
-
-
-
-
-
-
- [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);
- public static string GetMAC()
- {
-
- try
- {
- string strClientIP = HttpContext.Current.Request.UserHostAddress.ToString().Trim();
- Int32 ldest = inet_addr(strClientIP);
- Int32 lhost = inet_addr("");
- Int64 macinfo = new Int64();
- Int32 len = 6;
- int res = SendARP(ldest, 0, ref macinfo, ref len);
- string mac_src = macinfo.ToString("X");
- if (mac_src == "0")
- {
- if (strClientIP == "127.0.0.1")
- {
- return GetlMAC();
- }
- }
- while (mac_src.Length < 12)
- {
- mac_src = mac_src.Insert(0, "0");
- }
- string mac_dest = "";
- for (int i = 0; i < 11; i++)
- {
- if (0 == (i % 2))
- {
- if (i == 10)
- {
- mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));
- }
- else
- {
- mac_dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));
- }
- }
- }
- return mac_dest;
- }
- catch (Exception err)
- {
- return err.Message;
- }
- }
-
-
-
-
- public static string GetlMAC()
- {
- string stringMAC = "";
- ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");
- ManagementObjectCollection MOC = MC.GetInstances();
- foreach (ManagementObject MO in MOC)
- {
- if ((bool)MO["IPEnabled"] == true)
- {
- stringMAC += MO["MACAddress"].ToString();
- }
- }
- stringMAC = stringMAC.ToString().Replace(":", "-");
- return stringMAC;
- }