获取C#中的所有网络适配器 [英] Get all network adapters in C#

查看:396
本文介绍了获取C#中的所有网络适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。

如何使用C#获取所有网络适配器(启用和禁用)?

谢谢。



我的尝试:



NetworkInterface.GetIPProperties Method()

NetworkInterface.GetAllNetworkInterfaces Method()

推荐答案

尝试使用WMI查询硬件列表有用页面仅查找具有WMI Win32_NetworkAdapter类的物理网络适配器 [ ^ ]
Try using WMI to query the hardware list useful page here Find only physical network adapters with WMI Win32_NetworkAdapter class[^]


示例:

Example:
using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Linq.Expressions;
    using System.Management;
    using System.Threading;


        // Returns the list of Network Interfaces installed
        public static List<string> GetNicNames()
        {
            var nicNames = new List<string>();
            var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            var moc = mc.GetInstances();

            foreach (var mo in moc)
            {
                // if ((bool)mo["ipEnabled"])
                {
                    nicNames.Add(mo["Caption"].ToString());
                }
            }

            return nicNames;
        }


这篇关于获取C#中的所有网络适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆