我写了这段代码,用wmi禁用网络适配器:
public static bool DisableAdapter(string ConnectionName)
{
try
{
if (IsAdapterEnabled(ConnectionName))
{
SelectQuery wmiQuery =
new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionId != NULL");
ManagementObjectSearcher searchProcedure = new ManagementObjectSearcher(wmiQuery);
foreach (ManagementObject item in searchProcedure.Get())
{
if (((string) item["NetConnectionId"]).Match(ConnectionName, StringComparison.OrdinalIgnoreCase))
{
var ret=item.InvokeMethod("Disable", null);
}
}
}
return true;
}
catch (Exception)
{
return false;
}
}
在Windows 7上没问题,但在Windows 10上失败了!!!!! 我检查了安全性,invokmethod的返回值为' 0'
感谢。