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

简介: 原文:几种C#程序读取MAC地址的方法 以下是收集的几种C#程序读取MAC地址的方法,示例中是读取所有网卡的MAC地址,如果仅需要读取其中一个,稍作修改即可。 1 通过IPConfig命令读取MAC地址 ////// 根据截取ipconfig /all命令的输出流获取网卡Mac/////...
原文: 几种C#程序读取MAC地址的方法

 以下是收集的几种C#程序读取MAC地址的方法,示例中是读取所有网卡的MAC地址,如果仅需要读取其中一个,稍作修改即可。

1 通过IPConfig命令读取MAC地址

/// <summary>
/// 根据截取ipconfig /all命令的输出流获取网卡Mac
/// </summary>
/// <returns></returns>
public static 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>
public static 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)如果当前启用了多个网卡,最先返回的地址是最近启用的网络连接的信息
 
// 返回描述本地计算机上的网络接口的对象(网络接口也称为网络适配器)。
public static NetworkInterface[] NetCardInfo()
{
  return NetworkInterface.GetAllNetworkInterfaces();
}

/// <summary>
/// 通过NetworkInterface读取网卡Mac
/// </summary>
/// <returns></returns>
public static 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>
public static string 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 " )]
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);

5 从注册表读取MAC地址

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

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

 

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows Genuine Advantage

 

目录
相关文章
|
2月前
|
Java 调度 C#
C#学习系列相关之多线程(一)----常用多线程方法总结
C#学习系列相关之多线程(一)----常用多线程方法总结
|
2月前
|
C#
C#学习相关系列之数组---常用方法使用(二)
C#学习相关系列之数组---常用方法使用(二)
|
1天前
|
Web App开发 缓存 iOS开发
强制退出Mac程序的六种方法
强制退出Mac程序的六种方法
|
2天前
|
程序员 C#
C#抽象类和抽象方法详解
C#抽象类和抽象方法详解
6 0
|
2天前
|
存储 开发框架 .NET
C#中将DataTable转化成ListT的方法解析
C#中将DataTable转化成ListT的方法解析
5 0
|
3天前
|
数据采集 前端开发 数据挖掘
Fizzler库+C#:从微博抓取热点的最简单方法
本文介绍如何使用Fizzler库和C#构建微博热点信息爬虫。通过Fizzler的CSS选择器定位关键信息,提取热点标题和排名,实现微博内容的智能挖掘。示例代码展示单线程和多线程采集方法,并涉及代理IP使用。
Fizzler库+C#:从微博抓取热点的最简单方法
|
4天前
|
SQL 存储 Oracle
C#利用IDbCommand实现通用数据库脚本执行程序
C#利用IDbCommand实现通用数据库脚本执行程序
|
4天前
|
存储 数据采集 API
C# GetField 方法应用实例
C# GetField 方法应用实例
|
4天前
|
JSON 安全 API
C# GetMethod 方法应用实例
C# GetMethod 方法应用实例
|
10天前
Mac上IntelliJ IDEA设置类注释和方法注释带作者和日期
Mac上IntelliJ IDEA设置类注释和方法注释带作者和日期