2015-11-12 55 views
2

我想向用戶顯示當前網絡接口的mtu值。用戶可以通過netsh命令改變mtu,例如,對於ID爲11的接口:如何獲取C#中接口的當前MTU值

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

如何通過id或接口名稱讀取接口的當前MTU?

如果我使用System.Net.NetworkInformation命名空間的NetworkInterface類示例,則所有接口的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(); 
    } 
+2

*有效* MTU是所有網絡組件的最低MTU。當您嘗試1700時,您會很快達到此目標,您無法繞過下一步的基本MTU限制,以太網只能在一幀中攜帶1500個字節。 –

回答

0

我發現我自己的答案。我不得不改變MTU爲ipv6接口,如:

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

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