如何在C#中获取接口的当前MTU [英] How to get the current MTU of an interface in C#

查看:109
本文介绍了如何在C#中获取接口的当前MTU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to show the current network interface mtu value to the user. The user can change the mtu by the netsh command e.g. for the interface with id 11:

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

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

How can I read the current MTU of an interface by the id or interface name?

如果我使用 System.Net.NetworkInformation 命名空间中的 NetworkInterface 类示例,则所有接口的MTU均为1500.但是使用netsh命令(请参见上文)获得正确的MTU值,例如1700.

If I use the NetworkInterface class example from the System.Net.NetworkInformation namespace all interfaces have an MTU of 1500. But with the netsh command (see above) I get the correct MTU values of e.g. 1700.

这是示例:

https://msdn.microsoft.com/zh-CN/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,例如:

I've found the answer by myself. I have to change the MTU also for the ipv6 interface like:

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

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

这篇关于如何在C#中获取接口的当前MTU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆