获取本机的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
InBlock.gif
InBlock.gif        [DllImport("Iphlpapi.dll")]
InBlock.gif        private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
InBlock.gif
InBlock.gif        [DllImport("Ws2_32.dll")]
InBlock.gif        private static extern Int32 inet_addr(string ip);
InBlock.gif
InBlock.gif        public static string GetMacByARP(string clientIP)
ExpandedSubBlockStart.gif ContractedSubBlock.gif         dot.gif{
InBlock.gif            string ip = clientIP;
InBlock.gif            Int32 ldest = inet_addr(ip);
InBlock.gif            Int64 macinfo = new Int64();
InBlock.gif            Int32 len = 6;
InBlock.gif            try
ExpandedSubBlockStart.gif ContractedSubBlock.gif             dot.gif{
InBlock.gif                SendARP(ldest, 0, ref macinfo, ref len);
ExpandedSubBlockEnd.gif            }
InBlock.gif            catch
ExpandedSubBlockStart.gif ContractedSubBlock.gif             dot.gif{
InBlock.gif                return "";
ExpandedSubBlockEnd.gif            }
InBlock.gif            string originalMACAddress = Convert.ToString(macinfo, 16);
InBlock.gif            if (originalMACAddress.Length < 12)
ExpandedSubBlockStart.gif ContractedSubBlock.gif             dot.gif{
InBlock.gif                originalMACAddress = originalMACAddress.PadLeft(12, '0');
ExpandedSubBlockEnd.gif            }
InBlock.gif            string macAddress;
InBlock.gif            if (originalMACAddress != "0000" && originalMACAddress.Length == 12)
ExpandedSubBlockStart.gif ContractedSubBlock.gif             dot.gif{
InBlock.gif                string mac1, mac2, mac3, mac4, mac5, mac6;
InBlock.gif                mac1 = originalMACAddress.Substring(10, 2);
InBlock.gif                mac2 = originalMACAddress.Substring(8, 2);
InBlock.gif                mac3 = originalMACAddress.Substring(6, 2);
InBlock.gif                mac4 = originalMACAddress.Substring(4, 2);
InBlock.gif                mac5 = originalMACAddress.Substring(2, 2);
InBlock.gif                mac6 = originalMACAddress.Substring(0, 2);
InBlock.gif                macAddress = mac1 + "-" + mac2 + "-" + mac3 + "-" + mac4 + "-" + mac5 + "-" + mac6;
ExpandedSubBlockEnd.gif            }
InBlock.gif            else
ExpandedSubBlockStart.gif ContractedSubBlock.gif             dot.gif{
InBlock.gif                macAddress = "";
ExpandedSubBlockEnd.gif            }
InBlock.gif            return macAddress.ToUpper();
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        public static IPAddress[] GetLocalIP()
ExpandedSubBlockStart.gif ContractedSubBlock.gif         dot.gif{
InBlock.gif            string hostName = Dns.GetHostName();
InBlock.gif            IPHostEntry ipEntry = Dns.GetHostByName(hostName);
InBlock.gif            return ipEntry.AddressList;
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockEnd.gif        #endregion
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值