原文地址:http://www.cnblogs.com/diulela/archive/2012/04/07/2436111.html

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地址

///<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();
  }
}

[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. java读取配置文件的几种方法

    java读取配置文件的几种方法 原文地址:http://hbcui1984.iteye.com/blog/56496         在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配 ...

  2. C#中常用的读取xml的几种方法(转)

    本文完全来源于http://blog.csdn.net/tiemufeng1122/article/details/6723764,仅作个人学习之用. XML文件是一种常用的文件格式,例如WinFor ...

  3. windows读取mac格式移动硬盘的方法

    本文记录了一些window与mac数据在移动设备上互相拷贝的经验. 一.准备 家里有一台mac电脑,限于硬盘空间比较小,需要定期备份一些数据. 由于备份数据大小在20G左右,并且并没有压缩为一个压缩文 ...

  4. php 读取文件的几种方法

    文件操作的三个步骤,打开,操作,关闭.$fopen=fopen(路径,方式),fwrite($fopen,写入的字符串);fclose($fopen). 其中打开方式有如下几种方式: 模式 描述 r ...

  5. Shell按行读取文件的3种方法

    Shell按行读取文件的方法有很多,常见的三种方法如下: 要读取的文件: [root@mini05 -]# cat file.info 写法一: [root@mini05 -]# cat read1. ...

  6. 第十二节,TensorFlow读取数据的几种方法以及队列的使用

    TensorFlow程序读取数据一共有3种方法: 供给数据(Feeding): 在TensorFlow程序运行的每一步, 让Python代码来供给数据. 从文件读取数据: 在TensorFlow图的起 ...

  7. shell脚本,按行读取文件的几种方法。

    第一种方法用while实现按读取文件.[root@localhost wyb]# cat a.txt 第一行 aaaaaa 第二行 bbbbbb 第三行 cccccc 第四行 dddddd 第五行 e ...

  8. TensorFlow读取数据的三种方法

    tensortlfow数据读取有三种方式 placehold feed_dict:从内存中读取数据,占位符填充数据 queue队列:从硬盘读取数据 Dataset:同时支持内存和硬盘读取数据 plac ...

  9. [JavaWeb基础] 030.dom4j读取xml的4种方法

    通常我们在项目开发的过程中经常要操作到xml文件,在JAVA这边,我们会很自然的联想到Dom4J这个apache的开源插件,那么我们使用Dom4J如何来读取xml文件呢?下面我们来看看以下4种方法 1 ...

随机推荐

  1. oracle-sql模式匹配

    下面是条件 like与regexp_like条件 下面是函数 regexp_instr regexp_replace regexp_substr select * from tis_ft_user_i ...

  2. 用Hi3518EV200板当spi烧录器

    1. setenv bootargs setenv bootcmd 2.ddr烧录uboot 3.uboot下tftp下载文件 mw.b ff ;tftp ;sf erase ;sf write ; ...

  3. 寻找“最好”(4)——不等约束和KKT条件

    不等约束 上篇文章介绍了如何在等式约束下使用拉格朗日乘子法,然而真实的世界哪有那么多等式约束?我们碰到的大多数问题都是不等约束.对于不等约束的优化问题,可以这样描述: 其中f(x)是目标函数,g(x) ...

  4. react:路由登陆后才能访问的控制

    react-router 通过创建一个 需要认证的路由 来限制登陆后才能访问. 官方例子:https://reacttraining.com/react-router/web/example/auth ...

  5. Java反射之如何判断类或变量、方法的修饰符(Modifier解析)

    a->public b->public static c->public static final d->private 就是返回这些 https://blog.csdn.ne ...

  6. 读懂 PetaLinux:让 Linux 在 Zynq 上轻松起“跑”(转)

    对于Zynq这样一个“ARM+可编程逻辑”异构处理系统我们已经不陌生,其创新性大家也有目共睹.不过想要让更多的应用享受到这一“创新”带来的红利,让其真正“落地”则需要大量系统性的工作,去营造一个完善的 ...

  7. HanLP中人名识别分析详解

    HanLP中人名识别分析详解 在看源码之前,先看几遍论文<基于角色标注的中国人名自动识别研究> 关于命名识别的一些问题,可参考下列一些issue: l ·名字识别的问题 #387 l ·机 ...

  8. 使用openssl命令剖析RSA私钥文件格式

    原文 https://blog.csdn.net/zhymax/article/details/7683925 Openssl提供了强大证书功能,生成密钥对.证书,颁发证书.生成crl.验证证书.销毁 ...

  9. lsof根据端口返回进程号杀死进程的方法

    参考自:http://newmiracle.cn/?p=661 Linux shell根据端口返回进程号杀死进程的方法 kill -9 `lsof -t -i:8888` 这个就是杀死8888端口的进 ...

  10. RedHat6.5安装zookeeper单机

    版本号: Redhat6.5  zookeeper-3.4.6  JDK1.8 zookeeper下载 官网下载地址:https://mirrors.tuna.tsinghua.edu.cn/apac ...