简体   繁体   中英

C# WMI SetMTU System.ManagementException

We are working on a programm to configure some NICs. We have to change IP Adresses, Subnetmask and the MTU. Everything went well except the MTU Statement:

public void SetMTU()
{
    ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection objMOC = objMC.GetInstances();
    foreach (ManagementObject objMO in objMOC)
    {
       if (networkadapterID == (String)objMO["SettingID"])
          {                
               ManagementBaseObject setMTU;
               ManagementBaseObject newMTU = objMO.GetMethodParameters("SetMTU");
               Int32 test = 9216;
               newMTU["MTU"] = test;
               setMTU = objMO.InvokeMethod("SetMTU", newMTU, null);
          }  
    }
}

The correct NIC ID is given. Other WMI Operations succeed but we stuck on that one with error Message: System.Management.ManagementException: "Die Methode ist ungültig. "

(System.Management.ManagementException: "The Method is invalid.")

We have also tried to use "test" as string or uint32 (because the microsoft docs says it's an uint32),also:

newMTU["MTU"] = new (u)int[] { MTU };

but it doesnt work either. Meanwhile we don't have any ideas how to fix the problem.

I am grateful for every idea.

Thanks for your help and have a good day, Alex

Edit: Code to read the MTU should be (you have to tell this part a NetworkID so you don't read the Value of every NIC, you find this in your registry, but you should be able to delete the if part):

ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC)
{
    if (networkadapterID == (String)objMO["SettingID"])
    {
        MessageBox.Show(Convert.ToString(objMO["MTU"]) + ": " + Convert.ToString(objMO["SettingID"]));
    }
}

So far we didn't get the problem solved but we now check if the OS is Windows 10 and we will start a PowerShell task with this command:

Get-NetAdapterAdvancedProperty -Name 'NICName' -DisplayName 'Jumbo-Rahmen' | Set-NetAdapterAdvancedProperty -RegistryValue  'MTUsize';

Jumbo-Rahmen should be JumboPacket on an English OS

If the OS is not Windows 10 the User will be asked to change the MTU manually

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM