Disable/Enable Network Connections under Vista

Note: This is content from a blog originally published by Neil Cowburn in June of 2008, recovered with the aid of the wayback machine.


I got an email last week asking about how to disable a particular network connection under Vista. The specific scenario, how to disable an active 3G connection, is not something I’m going to cover, but what I present below could be used as basis for that scenario.

With Vista, Microsoft introduced two new methods to the Win32_NetworkAdapter class under WMI:Enable and Disable. Before can call either of those methods, we need to know how to enumerate the network connections.

The .NET Framework SDK provides a helpful utility called mgmtclassgen.exe, which can be used to create .NET-friendly wrappers of the WMI classes. Open up a Visual Studio command prompt and enter the following:

mgmtclassgen Win32_NetworkAdapter -p NetworkAdapter.cs

This will generate a file called NetworkAdapter.cs which will contain a C# representation of the WMI Win32_NetworkAdapter class. You can add this source code file to your C# project and then access all the properties without too much extra effort.

To filter and disable the specific adapters, you do something like this:

SelectQuery query = new SelectQuery("Win32_NetworkAdapter", "NetConnectionStatus=2");
ManagementObjectSearcher search = new ManagementObjectSearcher(query);
foreach(ManagementObject result in search.Get())
{
    NetworkAdapter adapter = new NetworkAdapter(result);

    // Identify the adapter you wish to disable here. 
    // In particular, check the AdapterType and 
    // Description properties.

    // Here, we're selecting the LAN adapters.
    if (adapter.AdapterType.Equals("Ethernet 802.3")) 
    {
        adapter.Disable();
    }
}

Don’t forget to add a reference to System.Management.dll!

4 thoughts on “Disable/Enable Network Connections under Vista”

  1. do you have anything more recent on this? The following does not work on my Windows 7 x64 computer. To test i was just trying to disable all of my Ethernet connections.

    SelectQuery query = new SelectQuery(“Win32_NetworkAdapter”, “AdapterTypeID=0”);
    ManagementObjectSearcher search = new ManagementObjectSearcher(query);
    foreach (ManagementObject result in search.Get())
    {
    NetworkAdapter adapter = new NetworkAdapter(result);
    adapter.Disable();
    }

    Like

  2. Ok i figured out what the issue was with Windows 7. With then new security you have to run this ‘As Administrator’ or it will not work.

    Like

  3. I can’t get the NetworkAdapter class to work. I have System.Management and System.Management.Instrumentation referenced and the using directives for both. Any thoughts?

    Like

    1. If you create the cs file and bring it in to your project, be sure to use the following using statement for the app to “see” the class:

      using ROOT.CIMV2.Win32;

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s