[英]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
類示例,則所有接口的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();
}
我自己找到了答案。 我還必須為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,則都返回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 subinterface“Local Area Connection”
首先我想,在啟用ipv6的情況下,只使用了ipv6-mtu大小,但事實並非如此。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.