如何检索IP v6子网掩码长度

时间:2012-08-06 18:43:28

标签: c# .net windows-7 registry ipv6

我正在编写一个必须在Windows 7计算机上获取网络适配器配置的应用程序,就像在Windows网络适配器配置面板中完成一样:

enter image description here

到目前为止,我可以从NetworkInterface.GetAllNetworkInterfaces() 除了子网前缀长度之外获得我需要的所有信息。

我知道可以通过PMIB_UNICASTIPADDRESS_TABLE从C ++ struc OnLinkPrefixLength检索它,但我试图留在.net。

我还查看了Win32_NetworkAdapterConfiguration WMI类,但它似乎只返回了IP v4子网掩码。

我也知道一些信息(不是前缀长度,据我所知)在注册表中:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\TCPIP6\Parameters\Interfaces\{CLSID}

我还使用SysInternals ProcessMon尝试在保存网络适配器设置时获得任何有用的东西,但什么都没找到......

那么,是否有任何干净的.NET方法来获取此值? (从注册表中获取它不会有问题)

编辑:网关

这与实际问题无关,但对于需要检索整个网络适配器IPv6配置的用户,IPInterfaceProperties.GatewayAdresses属性仅支持IPv4网关。正如下面的答案评论中提到的,在.NET Framework 4.5之前获取整个信息的唯一方法是调用WMI。

2 个答案:

答案 0 :(得分:4)

您可以使用Win32_NetworkAdapterConfiguration执行此操作。你可能忽略了它。

IPSubnet将返回一个字符串数组。使用第二个值。 我没有时间来编写一些C#代码,但我相信你可以处理它。使用WBEMTEST,我拉了这个:

instance of Win32_NetworkAdapterConfiguration
{
    Caption = "[00000010] Intel(R) 82579V Gigabit Network Connection";
    DatabasePath = "%SystemRoot%\\System32\\drivers\\etc";
    DefaultIPGateway = {"192.168.1.1"};
    Description = "Intel(R) 82579V Gigabit Network Connection";
    DHCPEnabled = TRUE;
    DHCPLeaseExpires = "20120808052416.000000-240";
    DHCPLeaseObtained = "20120807052416.000000-240";
    DHCPServer = "192.168.1.1";
    DNSDomainSuffixSearchOrder = {"*REDACTED*"};
    DNSEnabledForWINSResolution = FALSE;
    DNSHostName = "*REDACTED*";
    DNSServerSearchOrder = {"192.168.1.1"};
    DomainDNSRegistrationEnabled = FALSE;
    FullDNSRegistrationEnabled = TRUE;
    GatewayCostMetric = {0};
    Index = 10;
    InterfaceIndex = 12;
    IPAddress = {"192.168.1.100", "fe80::d53e:b369:629a:7f95"};
    IPConnectionMetric = 10;
    IPEnabled = TRUE;
    IPFilterSecurityEnabled = FALSE;
    IPSecPermitIPProtocols = {};
    IPSecPermitTCPPorts = {};
    IPSecPermitUDPPorts = {};
    IPSubnet = {"255.255.255.0", "64"};
    MACAddress = "*REDACTED*";
    ServiceName = "e1iexpress";
    SettingID = "{B102679F-36AD-4D80-9D3B-D18C7B8FBF24}";
    TcpipNetbiosOptions = 0;
    WINSEnableLMHostsLookup = TRUE;
    WINSScopeID = "";
};

IPSubnet [1] = IPv6子网;

编辑:这是一些代码。

StringBuilder sBuilder = new StringBuilder();
ManagementObjectCollection objects = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration").Get();
foreach (ManagementObject mObject in objects)
{
    string description = (string)mObject["Description"];
    string[] addresses = (string[])mObject["IPAddress"];
    string[] subnets = (string[])mObject["IPSubnet"];
    if (addresses == null && subnets == null)
        continue;
    sBuilder.AppendLine(description);
    sBuilder.AppendLine(string.Empty.PadRight(description.Length,'-'));
    if (addresses != null)
    {
        sBuilder.Append("IPv4 Address: ");
        sBuilder.AppendLine(addresses[0]);
        if (addresses.Length > 1)
        {
            sBuilder.Append("IPv6 Address: ");
            sBuilder.AppendLine(addresses[1]);
        }
    }
    if (subnets != null)
    {
        sBuilder.Append("IPv4 Subnet:  ");
        sBuilder.AppendLine(subnets[0]);
        if (subnets.Length > 1)
        {
            sBuilder.Append("IPv6 Subnet:  ");
            sBuilder.AppendLine(subnets[1]);
        }
    }
    sBuilder.AppendLine();
    sBuilder.AppendLine();
}
string output = sBuilder.ToString().Trim();
MessageBox.Show(output);

和一些输出:

Intel(R) 82579V Gigabit Network Connection
------------------------------------------
IPv4 Address: 192.168.1.100
IPv6 Address: fe80::d53e:b369:629a:7f95
IPv4 Subnet:  255.255.255.0
IPv6 Subnet:  64

编辑:我只是要澄清,以防有人稍后搜索。第二项并不总是IPv6值。 IPv4可以有多个地址和子网。在IPSubnet数组值上使用Integer.TryParse以确保它是IPv6子网和/或使用最后一项。

答案 1 :(得分:1)

使用参数解析netsh的输入流:

interface ipv6 show route

希望这有帮助!