我通过 Wan Miniport (PPPOE) 连接使用我的宽带互联网,并且我的操作系统是 Windows 7。我想通过 C# 断开互联网连接。我在互联网上搜索了很多,但我不确定哪种方法(WMI、WinInet 等)适合我的连接。稍后我将通过另一个软件重新连接,因此我的要求只是断开与互联网的连接,而不是完全永久禁用它。请提供一些解决方案和代码来实现这一点。?
问问题
1960 次
1 回答
0
使用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
于 2013-05-31T22:36:53.763 回答