Checking if IPv6 is enabled on Windows 7 using C#

半腔热情 提交于 2019-12-01 18:49:20

You can test whether the OS supports IPv6 by using this property:

bool supportsIpV6 = System.Net.Sockets.Socket.OSSupportsIPv6;

You can query for exactly what you asked for (if IPv6 is enabled or disabled for a specific network adapter) with the following code using the System.Net.NetworkInformation namespace:

using System.Net.NetworkInformation;

// ...

NetworkInterface[] allInterfaces = NetworkInterface.GetAllNetworkInterfaces();
NetworkInterface firstInterface = allInterfaces[0];
bool interfaceSupportsIPv6 = firstInterface.Supports(NetworkInterfaceComponent.IPv6);

Documentation on MSDN: Link

I have used this code to test it. Notice that it tests if the IPV6 is enabled, and not if the Network Card is IPV6 compatible:

public static bool InterfaceHasIpv6Enabled(NetworkInterface @interface)
{
  try
  {
    var properties = @interface.GetIPProperties().GetIPv6Properties();
    return properties.Index > -999;
  }
  catch (System.Net.NetworkInformation.NetworkInformationException)
  {
    return false;
  }
  catch (Exception ex)
  {
    throw ex;
  }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!