我需要以编程方式使用 C# 断开互联网连接吗? [英] Programmatically I need to disconnect my internet connection using C#?

查看:71
本文介绍了我需要以编程方式使用 C# 断开互联网连接吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 Wan Miniport (PPPOE) 连接使用我的宽带互联网,并且我的操作系统是 Windows 7.我想通过 C# 断开互联网连接.我在互联网上搜索了很多,但我不确定哪种方法(WMI、WinInet 等)适合我的连接.稍后我将通过另一个软件重新连接,因此我的要求只是与互联网断开连接,而不是完全永久禁用它.请给出一些解决方案&代码来实现这一点.?

I'm using my broadband internet through Wan Miniport (PPPOE) connection and I've Windows 7 as my OS. I would like to disconnect the internet connection through C#. I searched a lot over the internet but I'm not sure which methodology (WMI, WinInet etc) suits my connection. I will be reconnecting through another software later, hence my requirement is just to disconnect from the internet rather totally disabling it permanently. Kindly please give some solution & code to implement this. ?

推荐答案

使用 WMI:

C#:

var wmiQuery = new SelectQuery("SELECT * FROM Win32_NetworkAdapter " +
                                   "WHERE NetConnectionId != null " +
                                   "AND Manufacturer != 'Microsoft' ");
    using (var searcher = new ManagementObjectSearcher(wmiQuery))
    {
        foreach (ManagementObject item in searcher.Get())
        {
            if (((String)item["NetConnectionId"]) == "Local Area Connection")
            {
                using (item)
                {
                    item.InvokeMethod("Disable", null);
                }
            }
        }
    }

VB:

Dim wmiQuery = New SelectQuery("SELECT * FROM Win32_NetworkAdapter " & "WHERE NetConnectionId != null " & "AND Manufacturer != 'Microsoft' ")
Using searcher = New ManagementObjectSearcher(wmiQuery)
    For Each item As ManagementObject In searcher.[Get]()
        If DirectCast(item("NetConnectionId"), [String]) = "Local Area Connection" Then
            Using item
                item.InvokeMethod("Disable", Nothing)
            End Using
        End If
    Next
End Using

这篇关于我需要以编程方式使用 C# 断开互联网连接吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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