Hi Dw

Is there a way to disable/enable a selected NIC adapter using vb.net?
Thank you.

Recommended Answers

All 8 Replies

Hi

You can use Windows Management Instrumentation to do this. I'm not an expert in this area but I did find a very nice tool called WMI Code Creator that allows you to browse the WMI classes for both information and methods and then depending on what you want to do will also generate the code for you using VBScript, VB.NET or C#.

So for your example, I selected "Execute a Method" tab and in the drop down labelled "Classes with methods" I selected "Win32_NetworkAdapter". This then provides a list of all network interfaces on my machine and in the methods there was the option for Enabling and Disabling. I had to go through the list of network interfaces to find the right one but it did work (mine was ID 7) and Disabled/Enabled the device right from the tool.

You can probably use a mixture of techniques from the first tab "Query for data from a WMI class" to find the Network ID and then the method above.

The following was the code generated to disable the network card:

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
    public class CallWMIMethod
    {
        public static void Main()
        {
            try
            {
                ManagementObject classInstance = 
                    new ManagementObject("root\\CIMV2", 
                    "Win32_NetworkAdapter.DeviceID='7'",
                    null);

                // no method in-parameters to define


                // Execute the method and obtain the return values.
                ManagementBaseObject outParams = 
                    classInstance.InvokeMethod("Disable", null, null);

                // List outParams
                Console.WriteLine("Out parameters:");
                Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
            }
            catch(ManagementException err)
            {
                MessageBox.Show("An error occurred while trying to execute the WMI method: " + err.Message);
            }
        }
    }
}

HTH

Thanks. I now have a question on how can I embed or call this on my project where I have added a button to disable and another button to enable the adapter mean how do I put this within my project?

Do I have to make this as Function then call it on button click or?

Thank you again.

Hi

You can create this as a sub routine in your code and call it via your two buttons.

I have modified the code slightly so that it is easier to read and doesn't require one method for enabling and one for disabling:

    Private Sub SetNetworkCardStatus(ByVal nicId As Integer, ByVal enabled As Boolean)

        Try

            Dim classInstance As New ManagementObject("root\CIMV2", String.Format("Win32_NetworkAdapter.DeviceID='{0}'", nicId.ToString), Nothing)

            ' Execute the method and obtain the return values.
            Dim nicStatus As String = IIf(enabled = True, "enable", "disable")
            Dim outParams As ManagementBaseObject = classInstance.InvokeMethod(nicStatus, Nothing, Nothing)

        Catch err As ManagementException

            MessageBox.Show("An error occurred while trying to execute the WMI method: " & err.Message)

        End Try

    End Sub

Then to use the method, simply call via your buttons:

Private Sub enableButton_Click(sender As Object, e As EventArgs) Handles enableButton.Click

    SetNetworkCardStatus(7, True)

End Sub

Private Sub disableButton_Click(sender As Object, e As EventArgs) Handles disableButton.Click

    SetNetworkCardStatus(7, False)

End Sub

This can be added straight to your form code as a standard method or you can add it to a class or module.

Also note that you need to add a reference to System.Management in your project references and add the Imports System.Management line to the top of your code file.

HTH

Also I had a problem writing the code that was provided by the WMI Code Creater how do I write it, mean is it a class or?

Just edited my last post which hopefully answers your question.

Error with error message: Invalid method Parameter(s)

Ok I've figured out the error I had to change the IDs from 0 to 22 but I still didn't find my wifi adapter tried all but still the adapter wasn't turned off or disabled because at the moment it is enabled.

Is there perhaps another way to either get the adapter's ID or enable/disable the adapter with other method other then ID?

Hi

Use the following code (taken from the WMI Code Creator) to write all adapters (with their IDs) to the output window:

    Try
        Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_NetworkAdapter")

        For Each queryObj As ManagementObject In searcher.Get()

            Console.WriteLine("-----------------------------------")
            Console.WriteLine("Win32_NetworkAdapter instance")
            Console.WriteLine("-----------------------------------")
            Console.WriteLine("Caption: {0}", queryObj("Caption"))

        Next

    Catch err As ManagementException
        MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
    End Try

Unfortunately, I can't test in earnest as I am running Windows (and hence VB) via a VM which is bridged so I don't see a WiFi adaptor on my Windows instance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.