我想向用户显示当前的网络接口mtu值。用户可以通过netsh命令改变mtu,例如对于id为11的接口:
netsh interface ipv4 set subinterface 11 mtu=1700 store=persistent
如何通过ID或接口名称读取接口的当前MTU?
如果我使用NetworkInterface
命名空间中的System.Net.NetworkInformation
类示例,则所有接口的MTU均为1500.但是使用netsh命令(参见上文),我得到了正确的MTU值,例如: 1700。
这是一个例子:
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();
}
答案 0 :(得分:1)
我自己找到了答案。我还必须为ipv6界面更改MTU,如:
netsh interface ipv4 set subinterface 11 mtu=1700 store=persistent
netsh interface ipv6 set subinterface 11 mtu=1700 store=persistent
答案 1 :(得分:0)
主要问题是
的问题NetworkInterface.GetIPProperties().GetIPv4Properties().Mtu
NetworkInterface.GetIPProperties().GetIPv6Properties().Mtu
如果启用了ipv6,则两者均返回ipv6的mtu大小! 仅当禁用ipv6协议时,Mtu值才正确。
但是netsh显示正确的值
netsh interface ipv4 show subinterface "Local Area Connection"
netsh interface ipv6 show subinterface "Local Area Connection"
没有协议版本,将显示ipv4的值。 netsh interface ip show子接口“本地连接”
首先我想,在启用ipv6的情况下,仅使用ipv6-mtu大小,但这不是事实。