曾是土木人
曾是土木人

Reputation: 51

How to disable/enable network in Windows 8 by C#

This code works well on Windows 7, but not on Windows 8. Does anyone know why? I don't know how to solve it.

The function to restart network

    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;
            }
        }
    }

The function to disable the network

/// <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;
            }
        }

The function to enable the network

/// <summary>
        /// 启用网卡
        /// </summary>
        /// <param name="netWorkName">网卡名</param>
        /// <returns></returns>
        private static bool EnableNetWork(ManagementObject network)
        {
            try
            {
                network.InvokeMethod("Enable", null);
                return true;
            }
            catch
            {
                return false;
            }
        }

Upvotes: 4

Views: 3780

Answers (5)

hoyho
hoyho

Reputation: 21

my code works well in Windows 10 so i think win8 is available but remember that it needs administrator permission please remember run as admin by right click . here is my code:

             if (manage["Name"].ToString() == "Realtek RTL8192DE Wireless LAN 802.11N PCI-E NIC MAC1")
             {
                 Console.WriteLine(manage["Name"].ToString() + "\n");

                     try
                     {  
                         //先enable再disable且要管理员权限执行
                         manage.InvokeMethod("Enable", null);
                         manage.InvokeMethod("Disable", null);
                         Console.WriteLine("设置成功");                         
                     }
                     catch
                     {
                         Console.WriteLine("设置失败");
                     }                 
             }
        }

Upvotes: 2

Andr&#233; Dias
Andr&#233; Dias

Reputation: 173

Win32_NetworkAdapter is deprecated. For Windows 8 / Server 2012 and forward you need to use MSFT_NetAdapter. https://msdn.microsoft.com/en-us/library/hh968170(v=vs.85).aspx

Statement: "The Win32_NetworkAdapter class is deprecated. Use the MSFT_NetAdapter class instead." https://msdn.microsoft.com/en-us/library/aa394216%28v=vs.85%29.aspx

Upvotes: 0

user2399760
user2399760

Reputation: 27

I've just had the same issue. It turns out that when the same app I run as an administrator in Windows 8, everything started to work properly.

Upvotes: 0

mgrenier
mgrenier

Reputation: 1447

I found the answer to my comment and wanted to share for anyone having similar problems...

Rather than "Enabling" the service, I changed the start mode to manual (you can use automatic if you prefer as well) and that solved my issue.

ManagementBaseObject startMode = service.GetMethodParameters("ChangeStartMode");
startMode["startmode"] = "Manual";
service.InvokeMethod("ChangeStartMode", startMode, null);

This did the trick for me!

Upvotes: 1

Efran Cobisi
Efran Cobisi

Reputation: 6454

Assuming you are using the Win32_NetworkAdapter WMI class, make sure the current process is running in elevated mode. On top of that, you may want to just avoid catching every exception like you are doing and, instead, analyze the eventual exception which may be thrown, for additional details.

Upvotes: 2

Related Questions