> ## Content Index
> Fetch the complete content index at: https://blog.skitisu.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# C#获取所有适配器的名称、描述、状态、接口等
- URL: https://blog.skitisu.com/csharp-get-adapters-info/
- Published: 2014-03-28T11:59:35.000Z
- Updated: 2019-06-16T11:06:42.000Z
- Author: SkiTiSu
- Tags: 探索, 编程, #Import 2026-10-21 14:41

> 以下功能已全面整合在[TSAdapter类 – 通过C#获取/设置名称、状态、IP、DNS、网关、接口等信息](https://blog.skitisu.com/tsadapter-csharp-get-set-ip-dns-gateway-more "TSAdapter类 – 通过C#获取/设置名称、状态、IP、DNS、网关、接口等信息")，本文不再更新！

废话不多说，先引用需要用到的类

using System.Management;
using System.Net.NetworkInformation;
using System.Net;

> **大多数来这儿的人是想找获取接口的方法的吧。。。直接贴在这里了：**
> 
> NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
> foreach (NetworkInterface adapter in adapters)
> {
>    //。。。
>    IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
>    IPv4InterfaceProperties p = adapterProperties.GetIPv4Properties();
>    if (p != null)
>    {
>        某变量 = p.Index;
>    }
>    //。。。
> }

为了方便储存，定义了一个类

public class TSAdapter
{
    /// <summary>
    /// 适配器的名称
    /// </summary>
    public string Name;

    /// <summary>
    /// 适配器的描述
    /// </summary>
    public string Description;

    /// <summary>
    /// 适配器的服务名
    /// </summary>
    public string ServiceName;

    /// <summary>
    /// 适配器的状态
    /// </summary>
    public OperationalStatus Status;

    /// <summary>
    /// 适配器的类型
    /// </summary>
    public NetworkInterfaceType Type;

    /// <summary>
    /// 适配器的DNS服务器
    /// </summary>
    public List<string> DNS = new List<string>();

    /// <summary>
    /// 适配器的接口
    /// </summary>
    public UInt32? Interface;

    /// <summary>
    /// 适配器的网关
    /// </summary>
    public string Gateway;

    /// <summary>
    /// 重写的基类ToString方法
    /// </summary>
    /// <returns>适配器的名称</returns>
    public override string ToString()
    {
        return this.Name;
    }
}

下面是主要代码

/// <summary>
/// 获取所有适配器的状态、属性
/// </summary>
/// <returns>每个适配器的泛型</returns>
public static List<TSAdapter> GetAdapters()
{
    List<TSAdapter> back = new List<TSAdapter>();
    NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter");
    ManagementObjectCollection wmiadps = searcher.Get();

    foreach (NetworkInterface adapter in adapters)
    {
        TSAdapter adp = new TSAdapter();
        adp.Name = adapter.Name;
        adp.Description = adapter.Description;
        adp.Status = adapter.OperationalStatus;
        adp.Type = adapter.NetworkInterfaceType;

        IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
        //网关
        GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses;
        if (addresses.Count > 0)
        {
            adp.Gateway = addresses[0].Address.ToString();
        }

        //DNS
        IPAddressCollection dnsServers = adapterProperties.DnsAddresses;
        if (dnsServers.Count > 0)
        {
            foreach (IPAddress dns in dnsServers)
            {
                adp.DNS.Add(dns.ToString());
            }
        }

        //WMI中的数据
        foreach (ManagementObject mo in wmiadps)
        {
            if ((string)mo.GetPropertyValue("NetConnectionID") == adp.Name)
            {
                //接口(XP没有该属性!)
                try
                {
                    adp.Interface = (UInt32)mo.GetPropertyValue("InterfaceIndex");
                }
                catch
                {
                    adp.Interface = null;
                }
                //服务名称
                adp.ServiceName = (string)mo.GetPropertyValue("ServiceName");
            }
        }
        back.Add(adp);
    }
    return back;
}

这里有一个接口是干嘛用的捏？这是用来设置路由的，TS-NLB中将会用到\~  
至于IP，现在路由器这么普及，系统能识别到的都是DHCP指派的。可行的方法就是用那些上那个网站能显示IP的，把返回的网页拿来分析即可。以后有空来写一下。  
还有的是，这个类将有重大更新，支持set，敬请期待吧。

给一段测试的吧：

/// <summary>
/// 获取所有适配器的报告
/// </summary>
/// <returns>带有换行符的所有适配器的报告</returns>
public static string GetAdaptersReport()
{
    List<TSAdapter> adps = GetAdapters();
    string back = "";
    foreach (TSAdapter adp in adps)
    {
        back += "==================================================\r\n";
        back += "名称:" + adp.Name + "\r\n";
        back += "描述:" + adp.Description + "\r\n";
        back += "类型:" + adp.Type.ToString() + "\r\n";
        back += "状态:" + adp.Status.ToString() + "\r\n";
        back += "网关:" + adp.Gateway + "\r\n";
        foreach (string d in adp.DNS)
        {
            back += "DNS:" + d + "\r\n";
        }
        back += "接口:" + adp.Interface + "\r\n";
        back += "服务名:" + adp.ServiceName + "\r\n";
    }

    return back;
}

此代码段已收录在[TSLib](http://tslib.codeplex.com/?ref=blog.skitisu.com)，请遵循[Ms-PL许可证](http://tslib.codeplex.com/license?ref=blog.skitisu.com)（自由使用，声明出处）