您可以使用WMI。
添加System.Management
到您的引用并尝试此代码
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"]) == "Local Network Connection")
{
item.InvokeMethod("Disable", null);
}
}
还有另一篇文章:以编程方式禁用/启用网络连接。
WMI
您可以禁用和启用所有网络连接。
编辑:
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"select * from Win32_NetworkAdapter"))
{
ManagementObjectCollection results = searcher.Get();
foreach (ManagementObject obj in results)
{
System.Console.WriteLine("Found adapter {0} :", obj["Caption"]);
System.Console.WriteLine("Disabling adapter ...");
object[] param = new object[0];
obj.InvokeMethod("Disable",param);
System.Console.WriteLine("Done.");
}
Console.ReadLine();
}
请注意,某些适配器无法禁用。