如何在C#中获取接口的当前MTU

How to get the current MTU of an interface in C#

我想向用户显示当前网络接口的 mtu 值。用户可以通过 netsh 命令更改 mtu,例如对于 ID 为 11 的接口:

netsh interface ipv4 set subinterface 11 mtu=1700 store=persistent

如何通过 id 或接口名称读取接口的当前 MTU?

如果我使用 System.Net.NetworkInformation 命名空间中的 NetworkInterface class 示例,所有接口的 MTU 都是 1500。但是使用 netsh 命令(见上文)我得到了正确的 MTU例如的值1700.


这是例子:

https://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface(v=vs.110).aspx

public static void ShowNetworkInterfaces()
{
    IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
    Console.WriteLine("Interface information for {0}.{1}     ",
            computerProperties.HostName, computerProperties.DomainName);
    if (nics == null || nics.Length < 1)
    {
        Console.WriteLine("  No network interfaces found.");
        return;
    }

    Console.WriteLine("  Number of interfaces .................... : {0}", nics.Length);
    foreach (NetworkInterface adapter in nics)
    {
        IPInterfaceProperties properties = adapter.GetIPProperties();
        Console.WriteLine();
        Console.WriteLine(adapter.Description);
        Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length,'='));
        Console.WriteLine("  Interface type .......................... : {0}", adapter.NetworkInterfaceType);
        Console.WriteLine("  Physical Address ........................ : {0}", 
                   adapter.GetPhysicalAddress().ToString());
        Console.WriteLine("  Operational status ...................... : {0}", 
            adapter.OperationalStatus);
        string versions ="";

        // Create a display string for the supported IP versions.
        if (adapter.Supports(NetworkInterfaceComponent.IPv4))
        {
             versions = "IPv4";
         }
        if (adapter.Supports(NetworkInterfaceComponent.IPv6))
        {
            if (versions.Length > 0)
            {
                versions += " ";
             }
            versions += "IPv6";
        }
        Console.WriteLine("  IP version .............................. : {0}", versions);
        ShowIPAddresses(properties);

        // The following information is not useful for loopback adapters.
        if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback)
        {
            continue;
        }
        Console.WriteLine("  DNS suffix .............................. : {0}", 
            properties.DnsSuffix);

        string label;
        if (adapter.Supports(NetworkInterfaceComponent.IPv4))
        {
            IPv4InterfaceProperties ipv4 = properties.GetIPv4Properties();
            Console.WriteLine("  MTU...................................... : {0}", ipv4.Mtu);
            if (ipv4.UsesWins)
            {

                IPAddressCollection winsServers = properties.WinsServersAddresses;
                if (winsServers.Count > 0)
                {
                    label = "  WINS Servers ............................ :";
                    ShowIPAddresses(label, winsServers);
                }
            }
        }

        Console.WriteLine("  DNS enabled ............................. : {0}", 
            properties.IsDnsEnabled);
        Console.WriteLine("  Dynamically configured DNS .............. : {0}", 
            properties.IsDynamicDnsEnabled);
        Console.WriteLine("  Receive Only ............................ : {0}", 
            adapter.IsReceiveOnly);
        Console.WriteLine("  Multicast ............................... : {0}", 
            adapter.SupportsMulticast);
        ShowInterfaceStatistics(adapter);

        Console.WriteLine();
    }

我自己找到了答案。我还必须为 ipv6 接口更改 MTU,如:

netsh interface ipv4 set subinterface 11 mtu=1700 store=persistent

netsh interface ipv6 set subinterface 11 mtu=1700 store=persistent

主要问题是

的问题
NetworkInterface.GetIPProperties().GetIPv4Properties().Mtu
NetworkInterface.GetIPProperties().GetIPv6Properties().Mtu

如果启用了ipv6,两者return ipv6 的mtu 大小! Mtu 值只有在禁用 ipv6 协议时才是正确的。

但 netsh

显示了正确的值
netsh interface ipv4 show subinterface "Local Area Connection"
netsh interface ipv6 show subinterface "Local Area Connection"

没有协议版本,显示 ipv4 的值。 netsh 接口 ip 显示子接口 "Local Area Connection"

起初我以为,在启用ipv6的情况下,只使用ipv6-mtu大小,但事实并非如此。