C#获取所有适配器的名称、描述、状态、接口等

发布于 2014-03-28  1098 次阅读


以下功能已全面整合在 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,请遵循 Ms-PL 许可证(自由使用,声明出处)

寻找属于自己的1%