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