此代码在 Windows 7 上运行良好,但在 Windows 8 上运行良好。有人知道为什么吗?我不知道如何解决它。
重启网络功能
private static void RestartNetWork()
{
string manage = "SELECT * FROM Win32_NetworkAdapter";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(manage);
ManagementObjectCollection collection = searcher.Get();
List<string> netWorkList = new List<string>();
foreach (ManagementObject obj in collection)
{
if (obj["Name"].ToString() == "Qualcomm Atheros AR5B97 Wireless Network Adapter")
{
DisableNetWork(obj);//disable network
Thread.Sleep(3000);
EnableNetWork(obj);//enable network
return;
}
}
}
禁用网络功能
/// <summary>
/// 禁用网卡
/// </summary>5
/// <param name="netWorkName">网卡名</param>
/// <returns></returns>
private static bool DisableNetWork(ManagementObject network)
{
try
{
network.InvokeMethod("Disable", null);
return true;
}
catch
{
return false;
}
}
启用网络的功能
/// <summary>
/// 启用网卡
/// </summary>
/// <param name="netWorkName">网卡名</param>
/// <returns></returns>
private static bool EnableNetWork(ManagementObject network)
{
try
{
network.InvokeMethod("Enable", null);
return true;
}
catch
{
return false;
}
}