几种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

 

目录
相关文章
|
30天前
|
Java 调度 C#
C#学习系列相关之多线程(一)----常用多线程方法总结
C#学习系列相关之多线程(一)----常用多线程方法总结
|
30天前
|
C#
C#学习相关系列之数组---常用方法使用(二)
C#学习相关系列之数组---常用方法使用(二)
|
1月前
|
存储 C# 数据库
C# 生成唯一ID,有哪些方法?
【2月更文挑战第12天】
149 0
|
3月前
|
编译器 C# 开发者
C# 11.0中的新特性:覆盖默认接口方法
C# 11.0进一步增强了接口的灵活性,引入了覆盖默认接口方法的能力。这一新特性允许类在实现接口时,不仅可以提供接口中未实现的方法的具体实现,还可以覆盖接口中定义的默认方法实现。本文将详细介绍C# 11.0中接口默认方法覆盖的工作原理、使用场景及其对现有代码的影响,帮助开发者更好地理解和应用这一新功能。
|
3月前
|
C# 开发者
C# 9.0中的模块初始化器:程序启动的新控制点
【1月更文挑战第14天】本文介绍了C# 9.0中引入的新特性——模块初始化器(Module initializers)。模块初始化器允许开发者在程序集加载时执行特定代码,为类型初始化提供了更细粒度的控制。文章详细阐述了模块初始化器的语法、用途以及与传统类型初始化器的区别,并通过示例代码展示了如何在实际项目中应用这一新特性。
|
3月前
|
编译器 C# 开发者
C# 9.0中的顶级语句:简化程序入口的新特性
【1月更文挑战第13天】本文介绍了C# 9.0中引入的顶级语句(Top-level statements)特性,该特性允许开发者在不使用传统的类和方法结构的情况下编写简洁的程序入口代码。文章详细阐述了顶级语句的语法、使用场景以及与传统程序结构的区别,并通过示例代码展示了其在实际应用中的便捷性。
|
3月前
|
安全 C# 开发者
C#中的默认接口方法:接口演化的新篇章
【1月更文挑战第11天】本文探讨了C# 8.0中引入的默认接口方法,这一特性允许在接口中定义具有默认实现的方法。文章介绍了默认接口方法的语法、使用场景,以及它们如何影响接口的设计和实现,同时讨论了默认接口方法带来的好处和潜在的陷阱。
|
3月前
|
存储 C#
C# 方法详解:定义、调用、参数、默认值、返回值、命名参数、方法重载全解析
方法是一段代码,只有在调用时才会运行。 您可以将数据(称为参数)传递给方法。 方法用于执行某些操作,也被称为函数。 为什么使用方法?为了重用代码:定义一次代码,然后多次使用。
41 1
|
27天前
|
Java C# 开发工具
第一个C#程序
第一个C#程序
12 0
|
30天前
|
C#
C#学习相关系列之数据类型类----嵌套类和嵌套方法(三)
C#学习相关系列之数据类型类----嵌套类和嵌套方法(三)