Enable and disable IPv4 and IPv6 adapter programmatically in C #

Is it possible to enable / disable IPv4 and IPv6 for the selected network adapter using C # and .NET libraries or registry?

+4


source to share


3 answers


I tried the example How to disable IPv6 programmatically , but it doesn't work on my PC.

However, I found another way to solve the problem without editing the registry directly.

First, I'll show you how to solve this problem using PowerShell.

We have many ways to get the NIC name, for example, run the following command in PowerShell:

Get-NetAdapter

Let's assume the name of the network adapter is "Ethernet".

To enable IPv6, run the following command in PowerShell:

enable-NetAdapterBinding -Name 'Ethernet' -ComponentID ms_tcpip6

To disable IPv6, run the following command in PowerShell:



disable-NetAdapterBinding -Name 'Ethernet' -ComponentID ms_tcpip6

To get all ComponentIDs of a network card, run the following command in PowerShell:

Get-NetAdapterBinding -name 'Ethernet'

Now I will show you how to do this using C #.

1.Install the nuget package named "Microsoft.PowerShell.5.ReferenceAssemblies" in your project

2.If you want to disable IPv6 use the following code

using (var powerShell = PowerShell.Create())
{
    powerShell.AddScript("Disable-NetAdapterBinding -Name 'Ethernet' -ComponentID ms_tcpip6");
    powerShell.Invoke();
    if (powerShell.HadErrors)
    {
        // Failed, do something
        return;
    }
    // Success, do something
    return;
}

      

3. Now I believe that you already know how to perform other similar operations.

+1


source


Take a look at: http://eniackb.blogspot.com/2009/07/how-to-disable-ipv6-in-windows-2008.html



Basically modify the registry to do the same as using the GUI.

+1


source


You need to use the INetCfgBindingPath::Enable

then method INetCfg::Apply

(see here ). See Code Example: How to disable ipv6 programming code for details .

As far as I know nvspbind can disable or enable IPv6 on the specified adapter without restarting the PC. And I think it is nvspbind

based on the INetCfg

API.

-1


source







All Articles