Is it possible to Enable/Disable the IPv4 and IPv6 protocols for a selected network adapter using C# and .Net libraries or the registry?
I have 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.
We have many ways to get the name of NIC, for example execute the following command on PowerShell:
Get-NetAdapter
Let's assume the NIC name is "Ethernet".
To enable IPv6, execute the following command on PowerShell:
enable-NetAdapterBinding -Name 'Ethernet' -ComponentID ms_tcpip6
To disable IPv6, execute the following command on PowerShell:
disable-NetAdapterBinding -Name 'Ethernet' -ComponentID ms_tcpip6
To get all ComponentIDs of the NIC, execute the following command on PowerShell:
Get-NetAdapterBinding -name 'Ethernet'
1.Install the nuget package which named "Microsoft.PowerShell.5.ReferenceAssemblies" to 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 have already know how to perform other similar operations.