原文:几种C#程序读取MAC地址的方法

以下是收集的几种C#程序读取MAC地址的方法,示例中是读取所有网卡的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地址

    1)该方法依赖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地址

    1)如果当前的网卡是禁用状态(硬件处于硬关闭状态),取不到该网卡的MAC地址,(您可以通过禁用网卡进行试验)。
    2)如果当前启用了多个网卡,最先返回的地址是最近启用的网络连接的信息
 
//返回描述本地计算机上的网络接口的对象(网络接口也称为网络适配器)。
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地址

;
    }

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

[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);

5 从注册表读取MAC地址

常规用户可通过读取注册表项Windows Genuine Advantage获取到物理网卡地址。

1)如果注册表项被修改,则无法取得该MAC地址

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows Genuine Advantage

几种C#程序读取MAC地址的方法的更多相关文章

  1. C# 几种读取MAC地址的方法

    以下是收集的几种C#程序读取MAC地址的方法,示例中是读取所有网卡的MAC地址,如果仅需要读取其中一个,稍作修改即可. 1 通过IPConfig命令读取MAC地址 ///<summary> ...

  2. C#程序读取MAC地址的五种方法(转)

    public class GetMac { ///<summary> /// 根据截取ipconfig /all命令的输出流获取网卡Mac ///</summary> ///& ...

  3. C#中通过SendARP读取MAC地址

    C#中通过SendARP读取MAC地址: using System.Runtime.InteropServices; publicstaticstring GetMacBySendARP(string ...

  4. 修改MAC地址的方法 破解MAC地址绑定(抄)

    修改MAC地址的方法 破解MAC地址绑定   网卡的MAC地址是固化在网上EPROM中的物理地址,是一块网卡的“身份证”,通常为48位.在平常的应用中,有很多方面与MAC地址相关,如有些软件是和MAC ...

  5. Android 获取WIFI MAC地址的方法

    1. 经常用法,调用Android的API:WifiManager <uses-permission android:name="android.permission.ACCESS_W ...

  6. java获取服务器IP地址及MAC地址的方法

    这篇文章主要介绍了java编程实现获取机器IP地址及MAC地址的方法,实例分析了Java分别针对单网卡及多网卡的情况下获取服务器IP地址与MAC地址的相关技巧,需要的朋友可以参考下   本文实例讲述了 ...

  7. cubie两种固定MAC地址的方法

    1.修改 /etc/init.d/networking 配置文件 在(a)代码的后面添加上(b)这段代码 (a)case "$1" in start) :5e #MAC地址可改 2 ...

  8. C#通过WMI读取MAC地址

    该方法依赖WMI的系统服务,该服务一般不会被关闭;但如果系统服务缺失或者出现问题,该方法无法取得MAC地址,需要重启Windows Management Instrumentation服务. publ ...

  9. 手机电脑Mac地址修改方法

    1.什么是Mac地址? MAC(Media Access Control或者Medium Access Control)地址,意译为媒体访问控制,或称为物理地址.硬件地址,用来定义网络设备的位置.在O ...

随机推荐

  1. 使用IO流创建文件并写入数据

    /* 字符流和字节流: 字节流两个基类: InputStream OutputStream 字符流两个基类: Reader Writer 既然IO流是用于操作数据的, 那么数据的最常见体现形式是:文件 ...

  2. 关于Oracle表空间数据文件自增长的一些默认选项

    昨天,一个同事请教了一些关于Oracle表空间数据文件自增长的问题,解答过程中顺便整理起来,以后其他同事有同样的疑问时可以直接查阅. 实验内容: 创建MYTEST表空间,默认不开启自增长. 给MYTE ...

  3. java collections

    http://www.docjar.com/docs/api/java/util/technotes/guides/collections/changes4.html http://www.docja ...

  4. sql replace

    update dbo.EquipmentAttribute set AttributeName=replace(AttributeName,'    ','') where EquipmentID=8 ...

  5. UEFI+GPT 修复 win10启动

    要修复引导文件大致有以下几个步骤(按照我自己修复的步骤来的,其他情况可结合上面参考资料探索,大体思路应该没有变化): 挂载ESP分区 由于我的windows8.1已经无法进入了,Ubuntu不知道能不 ...

  6. C# 获取属性字段上DescriptionAttribute的值

    var ent = new Ent(); foreach (var item in ent.GetType().GetProperties()) { var v = (DescriptionAttri ...

  7. 给定一颗二叉搜索树,请找出其中的第k小的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。

    // ConsoleApplication2.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "stdafx.h ...

  8. [转载]c# OpenFileDialog

    string resultFile = ""; OpenFileDialog openFileDialog1 = new OpenFileDialog();            ...

  9. error X3025:global variables are implicitly constant, enable compatibility mode to allow modification

    global variables are implicitly constant, enable compatibility mode to allow modification http://xbo ...

  10. Deep Learning and Shallow Learning

    Deep Learning and Shallow Learning 由于 Deep Learning 现在如火如荼的势头,在各种领域逐渐占据 state-of-the-art 的地位,上个学期在一门 ...