C# 读取MAC地址的方法

1.通过IPConfig命令读取MAC地址

///<summary>
/// 根据截取ipconfig /all命令的输出流获取网卡Mac
///</summary>
///<returns></returns>
publicstatic List<string> GetMacByIPConfig()
{
  List<string> macs =new List<string>();

  ProcessStartInfo startInfo = new ProcessStartInfo("ipconfig", "/all");
  startInfo.UseShellExecute = false;
  startInfo.RedirectStandardInput = true;
  startInfo.RedirectStandardOutput = true;
  startInfo.RedirectStandardError = true;
  startInfo.CreateNoWindow = true;
  Process p = Process.Start(startInfo);
  //截取输出流
  StreamReader reader = p.StandardOutput;
  string line = reader.ReadLine();

  while (!reader.EndOfStream)
  {
    if (!string.IsNullOrEmpty(line))
    {
      line = line.Trim();

      if (line.StartsWith("Physical Address"))
      {
        macs.Add(line);
      }
    }


    line = reader.ReadLine();
  }

  //等待程序执行完退出进程
  p.WaitForExit();
  p.Close();
  reader.Close();
 
  return macs;
}


 

2.通过WMI读取MAC地址

///<summary>
/// 通过WMI读取系统信息里的网卡MAC
///</summary>
///<returns></returns>
publicstatic List<string> GetMacByWMI()
{
  List<string> macs =new List<string>();
  try
  {
    string mac ="";
    ManagementClass mc =new ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection moc = mc.GetInstances();
    foreach (ManagementObject mo in moc)
    {
      if ((bool)mo["IPEnabled"])
      {
        mac = mo["MacAddress"].ToString();
        macs.Add(mac);
      }
    }
    moc =null;
    mc =null;
  }
  catch
  {
  }

  return macs;
}

 

3.通过NetworkInterface读取MAC地址

//返回描述本地计算机上的网络接口的对象(网络接口也称为网络适配器)。
publicstatic NetworkInterface[] NetCardInfo()
{
  return NetworkInterface.GetAllNetworkInterfaces();
}

///<summary>
/// 通过NetworkInterface读取网卡Mac
///</summary>
///<returns></returns>
publicstatic List<string> GetMacByNetworkInterface()
{
  List<string> macs =new List<string>();
  NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
  foreach (NetworkInterface ni in interfaces)
  {
    macs.Add(ni.GetPhysicalAddress().ToString());
  }
  return macs;
}

 

4.通过SendARP读取MAC地址

[DllImport("Iphlpapi.dll")]
privatestaticexternint SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
[DllImport("Ws2_32.dll")]
privatestaticextern Int32 inet_addr(string ip);

///<summary>
/// 通过SendARP获取网卡Mac
/// 网络被禁用或未接入网络(如没插网线)时此方法失灵
///</summary>
///<param name="remoteIP"></param>
///<returns></returns>
publicstaticstring GetMacBySendARP(string remoteIP)
{
  StringBuilder macAddress =new StringBuilder();

  try
  {
    Int32 remote = inet_addr(remoteIP);

    Int64 macInfo =new Int64();
    Int32 length =6;
    SendARP(remote, 0, ref macInfo, ref length);

    string temp = Convert.ToString(macInfo, 16).PadLeft(12, '0').ToUpper();

    int x =12;
    for (int i =0; i <6; i++)
    {
      if (i ==5)
      {
        macAddress.Append(temp.Substring(x -2, 2));
      }
      else
      {
        macAddress.Append(temp.Substring(x -2, 2) +"-");
      }
      x -=2;
    }

    return macAddress.ToString();
  }
  catch
  {
    return macAddress.ToString();
  }
}

 

5.通过注册表读取MAC地址

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows Genuine Advantage 

 

posted @ 2013-04-09 15:58  青侠oO  阅读(2345)  评论(0编辑  收藏  举报