Programmatically I need to disconnect my internet connection using C#?

匿名 (未验证) 提交于 2019-12-03 01:09:02

问题:

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:

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 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!