[英]C# WMI SetMTU System.ManagementException
We are working on a programm to configure some NICs.我们正在开发一个程序来配置一些 NIC。 We have to change IP Adresses, Subnetmask and the MTU.
我们必须更改 IP 地址、子网掩码和 MTU。 Everything went well except the MTU Statement:
除了 MTU 声明外,一切都很顺利:
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.给出了正确的 NIC ID。 Other WMI Operations succeed but we stuck on that one with error Message: System.Management.ManagementException: "Die Methode ist ungültig. "
其他 WMI 操作成功,但我们坚持使用错误消息:System.Management.ManagementException:“Die Methode ist ungültig。”
(System.Management.ManagementException: "The Method is invalid.") (System.Management.ManagementException:“方法无效。”)
We have also tried to use "test" as string or uint32 (because the microsoft docs says it's an uint32),also:我们还尝试将“test”用作字符串或 uint32(因为 microsoft 文档说它是 uint32),还有:
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):编辑:读取 MTU 的代码应该是(您必须告诉这部分一个 NetworkID,这样您就不会读取每个 NIC 的值,您可以在注册表中找到它,但您应该能够删除 if 部分):
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:到目前为止,我们还没有解决问题,但我们现在检查操作系统是否为 Windows 10,我们将使用以下命令启动 PowerShell 任务:
Get-NetAdapterAdvancedProperty -Name 'NICName' -DisplayName 'Jumbo-Rahmen' | Set-NetAdapterAdvancedProperty -RegistryValue 'MTUsize';
Jumbo-Rahmen should be JumboPacket on an English OS Jumbo-Rahmen 应该是英语操作系统上的 JumboPacket
If the OS is not Windows 10 the User will be asked to change the MTU manually如果操作系统不是 Windows 10,将要求用户手动更改 MTU
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.