找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 136|回复: 0

[工具类] C# 获取网络适配器

[复制链接] IP属地:广东省广州市荔湾区
发表于 2024-6-8 10:19:01 | 显示全部楼层 |阅读模式
本帖最后由 shiy720 于 2024-6-8 10:23 编辑

C# 中有多种方式可以获取网络适配器,以下是一些常见的方法:

使用 System.Net.NetworkInformation 命名空间
  1. using System.Net.NetworkInformation;

  2. NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();

  3. foreach (NetworkInterface adapter in adapters)
  4. {
  5.     Console.WriteLine(adapter.Name);
  6.     Console.WriteLine(adapter.Description);
  7.     Console.WriteLine(adapter.Speed); // 速度
  8.     Console.WriteLine(adapter.GetPhysicalAddress()); // MAC 地址
  9.     Console.WriteLine(adapter.GetIPProperties()); // IP 配置
  10. }
复制代码

使用 System.Management 命名空间
  1. using System.Management;

  2. ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter");

  3. foreach (ManagementObject adapter in searcher.Get())
  4. {
  5.     Console.WriteLine(adapter["Name"]);
  6.     Console.WriteLine(adapter["Description"]);
  7.     Console.WriteLine(adapter["Speed"]); // 速度
  8.     Console.WriteLine(adapter["MACAddress"]); // MAC 地址
  9. }
复制代码

使用 System.Net.Sockets 命名空间
  1. using System.Net.Sockets;

  2. IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());

  3. foreach (IPAddress address in hostEntry.AddressList)
  4. {
  5.     Console.WriteLine(address.AddressFamily);
  6.     Console.WriteLine(address.ToString());
  7. }
复制代码

使用 WMI (Windows Management Instrumentation)
  1. using System.Management;

  2. ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");

  3. foreach (ManagementObject adapter in searcher.Get())
  4. {
  5.     Console.WriteLine(adapter["Caption"]);
  6.     Console.WriteLine(adapter["Description"]);
  7.     Console.WriteLine(adapter[" IPAddress"]); // IP 地址
  8.     Console.WriteLine(adapter["DefaultIPGateway"]); // 默认网关
  9.     Console.WriteLine(adapter["DNSDomain"]); // DNS 域
  10. }
复制代码

使用 System.Linq 和 System.Net.NetworkInformation 命名空间

  1. using System.Linq;
  2. using System.Net.NetworkInformation;

  3. NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();

  4. var networkAdapters = adapters.Select(adapter => new
  5. {
  6.     Name = adapter.Name,
  7.     Description = adapter.Description,
  8.     Speed = adapter.Speed,
  9.     MacAddress = adapter.GetPhysicalAddress(),
  10.     IpAddress = adapter.GetIPProperties().UnicastAddresses.FirstOrDefault().Address
  11. });

  12. foreach (var adapter in networkAdapters)
  13. {
  14.     Console.WriteLine(adapter.Name);
  15.     Console.WriteLine(adapter.Description);
  16.     Console.WriteLine(adapter.Speed);
  17.     Console.WriteLine(adapter.MacAddress);
  18.     Console.WriteLine(adapter.IpAddress);
  19. }
复制代码

这些方法可以获取到网络适配器的基本信息,如名称、描述、速度、MAC 地址、IP 地址等。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|西兴社区 ( 蜀ICP备2022005627号 )|网站地图

GMT+8, 2024-12-27 08:46 , Processed in 0.619673 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表