Programmatically I need to disconnect my internet

2019-08-28 22:55发布

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. ?

1条回答
Animai°情兽
2楼-- · 2019-08-28 23:20

Use 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
查看更多
登录 后发表回答