Vb.net - 显示每个网络适配器,启用或禁用

发布于 2024-10-15 02:13:45 字数 846 浏览 2 评论 0原文

我正在尝试显示运行此应用程序的计算机上的网络适配器。 为此,我希望能够显示所有网络适配器,无论它们处于哪种状态。 然后我希望能够通过按下按钮来启用和禁用适配器。

我尝试过使用 System.Net.NetworkInformation,但似乎它只包含活动的信息。

' NETWORK ADAPTERS

' Create label
Dim LabelNetworkAdapter As New Label
Dim old As Padding = LabelNetworkAdapter.Margin
LabelNetworkAdapter.Margin = New Padding(old.Left, 8, old.Right, old.Bottom)

' CreateButton
Dim BtnConnectButton As New Button
BtnConnectButton.Height = 23
BtnConnectButton.Width = 60

For Each nic As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()

    Dim strText As String = nic.Description.ToString
    TableLayoutPanel_Nettverkskort.Controls.Add(LabelNetworkAdapter)
    LabelNetworkAdapter.Text = strText

    TableLayoutPanel_Nettverkskort.Controls.Add(BtnConnectButton)
    BtnConnectButton.Text = "Koble fra"

    Exit For

Next

I'm trying to display the network adapters on the computer running this application.
For that I want to be able to display all network adapters, no matter which status they have.
Then I want to be able to enable and disable the adapters by pushing a button.

I've tried using System.Net.NetworkInformation, but it seems as though it only contains the one that's active.

' NETWORK ADAPTERS

' Create label
Dim LabelNetworkAdapter As New Label
Dim old As Padding = LabelNetworkAdapter.Margin
LabelNetworkAdapter.Margin = New Padding(old.Left, 8, old.Right, old.Bottom)

' CreateButton
Dim BtnConnectButton As New Button
BtnConnectButton.Height = 23
BtnConnectButton.Width = 60

For Each nic As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()

    Dim strText As String = nic.Description.ToString
    TableLayoutPanel_Nettverkskort.Controls.Add(LabelNetworkAdapter)
    LabelNetworkAdapter.Text = strText

    TableLayoutPanel_Nettverkskort.Controls.Add(BtnConnectButton)
    BtnConnectButton.Text = "Koble fra"

    Exit For

Next

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜味拾荒者 2024-10-22 02:13:45

你想做的事是不可能的。当您禁用网络接口时,操作系统会对应用程序隐藏它。这是有道理的,因为您实际上无法使用它。您看到它的唯一方法是在 shell 的“网上邻居”文件夹中(或者他们现在决定称呼它的任何名称)。

MSDN 上的此错误报告似乎恰当地描述您的问题。它被“按设计”关闭。

文档甚至解释了返回值为:

一个 NetworkInterface 数组,其中包含描述可用网络接口的对象,如果未检测到接口,则为空数组。

What you're trying to do is not possible. When you disable a network interface, the operating system hides it from applications. Which makes sense, since you can't actually use it. The only way you're going to see it is in the shell's "My Network Places" folder (or whatever they've decided to call it now).

This bug report on MSDN seems to aptly describe your problem. It was closed as "by design".

The documentation even explains that the return value is:

A NetworkInterface array that contains objects that describe the available network interfaces, or an empty array if no interfaces are detected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文