问题描述
我要编写一个简单的网络配置工具,可以设置IP地址等静态还是让它自动(DHCP)设置,全部由WMI的手段。
不幸的是,设置地址静态的作品只有一次!所以,当你运行测试功能下一次,一切都运行完美(不要忘了一个断点//动态!)。但在第二次,当你检查结果的网络适配器的IP地址和子网掩码的控制面板的属性页保持为空!当然也有没有异常抛出和方法调用的结果总是零(0)。在code的课程上以管理员两种不同的Windows 7机器和测试。
无效测试()
{
//查找管理对象
ManagementClass networkManagementClass =新ManagementClass(Win32_NetworkAdapterConfiguration的);
ManagementObjectCollection networkManagement = networkManagementClass.GetInstances();
的ManagementObject适配器= NULL;
的foreach(的ManagementObject莫networkManagement)
{
如果((布尔)莫[IPEnabled]&功放;及(串)莫[标题] ==[00000012]英特尔(R)82577LM千兆网络连接)
{
适配器=月;
打破;
}
}
// STATIC
VAR VAL = adapter.InvokeMethod(EnableStatic的,新的对象[] {
新的String [] {192.168.1.99},
新的String [] {255.255.255.0}
});
VAL = adapter.InvokeMethod(SetGateways,新的对象[] {
新的String [] {192.168.1.254},
新UINT16 [] {1}
});
VAL = adapter.InvokeMethod(SetDNSServerSearchOrder,新的对象[] {
新的String [] {192.168.1.254}
});
// 动态
adapter.InvokeMethod(SetDNSServerSearchOrder,新的对象[] {新的字符串[0]});
adapter.InvokeMethod(enable,使能,新的对象[] {});
}
最后,我想通了,对于这样的解决方法(我猜)的Windows错误:请填写正确的价值观到注册表中直接之前WMI调用:
//之窗错误的解决方法(窗口拒绝申请静态IP网络的属性对话框)
变种settingID = adapter.GetPropertyValue(SettingID); //适配器=管理对象
使用(VAR REGKEY = Registry.LocalMachine.OpenSubKey(@系统\ CurrentControlSet \服务\ TCPIP \参数\接口\+ settingID,真))
{
regKey.SetValue(enable,使,0);
regKey.SetValue(ip地址,networkState.IPAddress,RegistryValueKind.MultiString);
regKey.SetValue(子网掩码,networkState.SubnetMask,RegistryValueKind.MultiString);
}
工程就像一个魅力对我来说。玩得开心:)
I'm about to write a simple network configuration tool which can either set IP address and so on statically or let it be set automatically (DHCP), all by means of WMI.
Unfortunately, setting the address statically works just once! So when you run the test function below once, everything works perfect (don't forget a breakpoint at // DYNAMIC!). But at the second time and when you check the results in the control panel's properties page of the network adapter IP address and subnet mask remain empty! Of course there are no exceptions thrown and the result of the method invocation is always zero (0). The code was tested on two different Windows 7 machines and of course as administrator.
void Test()
{
// find management object
ManagementClass networkManagementClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection networkManagement = networkManagementClass.GetInstances();
ManagementObject adapter = null;
foreach (ManagementObject mo in networkManagement)
{
if ((bool)mo["IPEnabled"] && (string)mo["Caption"] == "[00000012] Intel(R) 82577LM Gigabit Network Connection")
{
adapter = mo;
break;
}
}
// STATIC
var val = adapter.InvokeMethod("EnableStatic", new object[] {
new string[] { "192.168.1.99" },
new string[] { "255.255.255.0" }
});
val = adapter.InvokeMethod("SetGateways", new object[] {
new string[] { "192.168.1.254" },
new UInt16[] { 1 }
});
val = adapter.InvokeMethod("SetDNSServerSearchOrder", new object[] {
new string[] { "192.168.1.254" }
});
// DYNAMIC
adapter.InvokeMethod("SetDNSServerSearchOrder", new object[] { new string[0] });
adapter.InvokeMethod("EnableDHCP", new object[] { });
}
Finally I figured out a workaround for this (i guess) Windows bug: Fill the right values into the registry DIRECTLY and BEFORE the WMI calls:
// workaround of windows bug (windows refused to apply static ip in network properties dialog)
var settingID = adapter.GetPropertyValue("SettingID"); // adapter = the management object
using (var regKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\" + settingID, true))
{
regKey.SetValue("EnableDHCP", 0);
regKey.SetValue("IPAddress", networkState.IPAddress, RegistryValueKind.MultiString);
regKey.SetValue("SubnetMask", networkState.SubnetMask, RegistryValueKind.MultiString);
}
Works like a charm for me. Have fun :)
这篇关于为什么采用静态IP地址通过WMI工作只有一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!