以编程方式断开网络连接有没有办法在.NET4.0中以编程方式暂时断开网络连接?我知道我可以通过做...System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()来获取当前的网络连接状态但是出于测试目的,我想测试我的应用程序在失去网络连接时的行为(无需物理拔下网络电缆).谢谢,克里斯。您可以使用WMI执行此操作。这就是我们用来禁用物理适配器来测试这些类型的场景。使用系统管理;使用System.Linq;命名空间DisableNIC{内部静态类程序{privatestaticvoidMain(){varwmiQuery=newSelectQuery("SELECT*FROMWin32_NetworkAdapter"+"WHERENetConnectionId!=null"+"ANDManufacturer!='Microsoft'");使用(varsearcher=newManagementObjectSearcher(wmiQuery)){foreach(variteminsearcher.Get().OfType()){if((string)item["NetConnectionId"]!="LocalAreaConnection")continue;使用(项目){item.InvokeMethod(“禁用”,空);}}}}}}您没有指定操作系统,但这适用于Windows7和Windows8。请注意,您需要是管理员才能运行此功能。如果您使用“ManagedWifiAPI”,您只需删除配置文件。这对我有用。WlanClient客户端=newWlanClient();WlanClient.WlanInterfacem_WlanInterface=client.Interfaces.Where(i=>i.InterfaceDescription.Contains(InterfaceIdentifierString)).First();m_WlanInterface.DeleteProfile(ConnectionProfileString);如果需要重新连接网络,请务必保存xml配置文件:stringxmlString=m_WlanInterface.GetProfileXml(ConnectionProfileString)然后就可以复用了更多关于C#的学习教程,希望大家多多关注—m_WlanInterface.SetProfile(Wlan.WlanProfileFlags.AllUser,xmlString,true);m_WlanInterface.Connect(Wlan.WlanConnectionMode.Profile,Wlan.Dot11BssType.Any,ConnectionProfileString);本文来自网络收藏,不代表立场,如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处:
