问题
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
How can I read the current MTU of an interface by the id or interface name?
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.
This is the example:
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();
}
回答1:
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
回答2:
The main problem is a problem of
NetworkInterface.GetIPProperties().GetIPv4Properties().Mtu
NetworkInterface.GetIPProperties().GetIPv6Properties().Mtu
If ipv6 is enabled, both return the mtu size of ipv6! The Mtu value is only correct if the ipv6 protocol is disabled.
But the correct values are shown by netsh
netsh interface ipv4 show subinterface "Local Area Connection"
netsh interface ipv6 show subinterface "Local Area Connection"
Without protocol version, the value for ipv4 is shown. netsh interface ip show subinterface "Local Area Connection"
First I thought, that in the case of enabling ipv6, only the ipv6-mtu size is used, but that's not the fact.
来源:https://stackoverflow.com/questions/33666933/how-to-get-the-current-mtu-of-an-interface-in-c-sharp