有没有办法获取C#中禁用/启用的网络接口的列表 [英] Is there a way to get a list of disabled/enabled network interfaces in C#

查看:362
本文介绍了有没有办法获取C#中禁用/启用的网络接口的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小脚本来更改特定Wireless80211或以太网网络接口的IPv4地址.到目前为止,一切正常.该脚本使用命令提示符和netsh设置IPv4(要使用C#控制它,我使用System.Diagnostics). 我要添加此功能,该脚本将禁用或启用所有"Wireless80211"和以太网"接口(没有特定接口),您可以在控制面板>网络和Internet>网络连接"中找到它.

I am programming a little script to change the IPv4 address of a specific Wireless80211 or Ethernet network interface. So far everything is working fine. The script sets the IPv4 with the command prompt and netsh (to control it with C# I use System.Diagnostics). I want to add the feature, that the script disables or enables all Wireless80211 and Ethernet network interfaces (without a specific one) which you can find in "Control Panel>Network and Internet>Network Connections".

该脚本主要用于ArtNet到DMX,以自动准备要使用ArtNet的所有内容(对于不了解控制面板"并自动工作流程的人们). 我已经使用System.Net.NetworkInformation命名空间进行了尝试,但是我只找到了一种获取启用的网络接口的方法.禁用接口后,System.Net.NetworkInformation不会显示此接口.

The script is mainly used for ArtNet to DMX to automatically prepare everything to use ArtNet (for people, which do not know anything of the Control Panel and to automate the workflow). I have already tried it with the System.Net.NetworkInformation namespace, but I have only found a way to get enabled network interfaces. As soon as I disable an interface System.Net.NetworkInformation does not show this interface.

推荐答案

我不知道NetworkInterface.GetAllNetworkInterfaces()没有返回禁用的接口.

I wasn't aware that NetworkInterface.GetAllNetworkInterfaces() didn't return disabled interfaces.

无论如何,您可以尝试通过.NET框架中可用的System.Management.dll使用WMI api(必须将此引用添加到您的项目中),我进行了测试,它甚至允许与禁用的网络接口进行交互.

Anyway, you could try using the WMI api via System.Management.dll that's available in the .NET framework (you must add this reference to your project), I did a test and it allows you to interact even with disabled network interfaces.

以下示例为您提供了如何通过此api使用WMI的想法,我几乎从文档中提取了它:

The following example give you an idea of how to work with WMI via this api, I pretty much extracted it from the documentation:

using System;
using System.Management;
...
void ListNetworkAdapters()
{
    var query = new ObjectQuery("SELECT * FROM Win32_NetworkAdapter");

    using (var searcher = new ManagementObjectSearcher(query))
    {
        var queryCollection = searcher.Get();

        foreach (var m in queryCollection)
        {
            Console.WriteLine("ServiceName : {0}", m["Name"]);
            Console.WriteLine("MACAddress : {0}", m["Description"]);
            Console.WriteLine();
        }

        Console.ReadLine();
    }
}

可以在这里找到文档: https://docs.microsoft.com/zh-cn/Windows/桌面/cimwin32prov/win32-networkadapter

The documentation can be found here: https://docs.microsoft.com/en-us/windows/desktop/cimwin32prov/win32-networkadapter

这篇关于有没有办法获取C#中禁用/启用的网络接口的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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