4

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;
            }
        }
4
  • The InvokeMethod("Enable", null); does not work for me, I get an error saying no such method...am I doing something wrong? InvokeMethod("StartService", null, null); works fine for me though Commented Dec 11, 2013 at 20:57
  • Just to add to my comment, I am running Windows 2003 Server SP2 Commented Dec 12, 2013 at 14:50
  • For me neither InvokeMethod("Enable", null); or InvokeMethod("StartService", null, null); works on Windows 10 :-( Commented Jul 25, 2016 at 23:51
  • Found the solution. When I'd disabled the adapter it dropped out of the query results... Needed to do a new SelectQuery looking disabled adapters! ie. Dim query As New SelectQuery("Win32_NetworkAdapter", "NetConnectionStatus=0") After that the enable method worked. Commented Jul 25, 2016 at 23:56

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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("设置失败");
                     }                 
             }
        }

Comments

1

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!

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.