• 获取本机的MAC的方法


    1。用C#调用DOS命令获取本机网卡的MAC地址
    System.Diagnostics.Process p=new System.Diagnostics.Process();
    p.StartInfo.CreateNoWindow=true;
    p.StartInfo.UseShellExecute=false;
    p.StartInfo.RedirectStandardOutput=true;
    p.StartInfo.FileName="ipconfig";
    p.StartInfo.Arguments="/all";
    p.Start();
    p.WaitForExit();
    string s=p.StandardOutput.ReadToEnd();
    MessageBox.Show(s.Substring(s.IndexOf("Physical Address. . . . . . . . . :")+36,17));

    2。使用API,利用ARP协议,只能获得同网段计算机的MAC


         #region By ARP

            [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 GetMacByARP(string clientIP)
            {
                string ip = clientIP;
                Int32 ldest = inet_addr(ip);
                Int64 macinfo = new Int64();
                Int32 len = 6;
                try
                {
                    SendARP(ldest, 0, ref macinfo, ref len);
                }
                catch
                {
                    return "";
                }
                string originalMACAddress = Convert.ToString(macinfo, 16);
                if (originalMACAddress.Length < 12)
                {
                    originalMACAddress = originalMACAddress.PadLeft(12, '0');
                }
                string macAddress;
                if (originalMACAddress != "0000" && originalMACAddress.Length == 12)
                {
                    string mac1, mac2, mac3, mac4, mac5, mac6;
                    mac1 = originalMACAddress.Substring(10, 2);
                    mac2 = originalMACAddress.Substring(8, 2);
                    mac3 = originalMACAddress.Substring(6, 2);
                    mac4 = originalMACAddress.Substring(4, 2);
                    mac5 = originalMACAddress.Substring(2, 2);
                    mac6 = originalMACAddress.Substring(0, 2);
                    macAddress = mac1 + "-" + mac2 + "-" + mac3 + "-" + mac4 + "-" + mac5 + "-" + mac6;
                }
                else
                {
                    macAddress = "";
                }
                return macAddress.ToUpper();
            }

            public static IPAddress[] GetLocalIP()
            {
                string hostName = Dns.GetHostName();
                IPHostEntry ipEntry = Dns.GetHostByName(hostName);
                return ipEntry.AddressList;
            }

            #endregion
  • 相关阅读:
    js格式化货币金额
    Mac idea maven 创建web项目
    The string "--" is not permitted within comments.
    Java从数据库读取页面树形菜单
    oracle 查询重复数据并且删除, 只保留一条数据重复数据
    SSM框架---使用MyBatis Generator自动创建代码
    前端 NPM常用命令行
    前端常用命令行
    Angular 常用命令行
    JS/JQuery 文档加载完成
  • 原文地址:https://www.cnblogs.com/tuyile006/p/566693.html
Copyright © 2020-2023  润新知