我正在尝试创建一个工具,将动态DHCP提供的IPv4地址,网关和dns设置转换为静态配置。我曾尝试使用WMI来解决这个难题,但我有一个我无法弄清楚的问题。
应用程序完成,DNS和网关已配置,但EnableStatic
方法(设置IP地址和子网)不成功,这意味着仍然从DHCP(带有灰色字段)接收IP虽然已设置默认网关。我该如何解决这个问题?
EnableStatic的ReturnValue为70(无效的IP地址)。奇怪的是输入参数与我2秒前从NIC中提取的输入参数相同。
以下是代码(GUI除外),http://pastebin.com/AE3dGhUz:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
namespace Static_NIC_Settings_Creator
{
public partial class Form1 : Form
{
private ManagementObjectCollection queryCollection;
private string[] networkInterfaces;
private int currentNIC;
private string[] ipAddress;
private string[] subnetMask;
private string[] defaultIPGateway;
private string[] dnsServerSearchOrder;
public Form1()
{
InitializeComponent();
getNICs();
}
private void convertButton_Click(object sender, EventArgs e)
{
if (networkInterfaces.Count() > 0)
{
//Get current NIC settings
if (!getNICSettings())
{
MessageBox.Show("Retrieving current NIC settings failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//Convert to static NIC settings
if (!setNICStatic())
{
MessageBox.Show("Setting NIC settings to static failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
private void nicSelecter_SelectedIndexChanged(object sender, EventArgs e)
{
currentNIC = nicSelecter.SelectedIndex;
}
private void getNICs()
{
//Get NICS
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");
queryCollection = query.Get();
//Make nic string array
int i = queryCollection.Count;
networkInterfaces = new string[i];
//Fill nic string array
i = 0;
foreach (ManagementObject mo in queryCollection)
{
networkInterfaces[i] = (String)mo["Description"];
i++;
}
//Fill dropbox with arraylist-data
nicSelecter.DataSource = networkInterfaces;
}
private Boolean getNICSettings()
{
//Get selected NIC
int i = 0;
foreach (ManagementObject mo in queryCollection)
{
//Get settings for specific NIC
if (i == currentNIC)
{
try
{
ipAddress = (String[])mo["IPAddress"];
subnetMask = (String[])mo["IPSubnet"];
defaultIPGateway = (String[])mo["DefaultIPGateway"];
dnsServerSearchOrder = (String[])mo["DNSServerSearchOrder"];
return true;
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString(), "Critical: Unhandled error");
return false;
}
}
i++;
}
return false;
}
private Boolean setNICStatic()
{
//Get selected NIC
int i = 0;
foreach (ManagementObject mo in queryCollection)
{
//Get settings for specific NIC
if (i == currentNIC)
{
try
{
//Set static IP and subnet mask
ManagementBaseObject setIP;
ManagementBaseObject newIP = mo.GetMethodParameters("EnableStatic");
newIP["IPAddress"] = ipAddress;
newIP["SubnetMask"] = subnetMask;
setIP = mo.InvokeMethod("EnableStatic", newIP, null);
//Set default gateway
ManagementBaseObject setGateway;
ManagementBaseObject newGateway = mo.GetMethodParameters("SetGateways");
newGateway["DefaultIPGateway"] = defaultIPGateway;
newGateway["GatewayCostMetric"] = new int[] { 1 };
setGateway = mo.InvokeMethod("SetGateways", newGateway, null);
//Set dns servers
ManagementBaseObject setDNS;
ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder");
newDNS["DNSServerSearchOrder"] = dnsServerSearchOrder;
setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);
System.Windows.Forms.MessageBox.Show("Setting NIC settings returned: " + setDNS);
return true;
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString(), "Critical: Unhandled error");
return false;
}
}
i++;
}
//No NICs
return false;
}
} //End class
}
有什么想法吗?
答案 0 :(得分:3)
您是否也在输入IPv6地址?只是玩PowerShell似乎不喜欢它们。也许您可以发布在调试时输入的实际值,这将有很大帮助。也可以尝试静态输入一些值,如:
new string[]{"192.168.0.1"}, new string[] {"255.255.255.255"}
除非你真的需要C#和GUI,否则你可能需要考虑使用PowerShell(当然要求它是安装的)因为WMI在那里操作起来要简单得多(遗憾的是你仍然有学习曲线)。
这只是如何使用PowerShell的示例,您至少可以将其用于某些测试:
Get-WmiObject Win32_NetworkAdapterConfiguration
然后获取适配器的索引然后运行,但替换索引号:
$obj = Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.Index -eq 1}
$obj.EnableStatic("192.168.0.1", "255.255.255.0")
要获取方法参数,请运行:
$obj.EnableStatic
它将返回:
MemberType : Method
OverloadDefinitions : {System.Management.ManagementBaseObject EnableStatic(System.String[]IPAddress, System.String[] SubnetMask)}
TypeNameOfValue : System.Management.Automation.PSMethod
Value : System.Management.ManagementBaseObject EnableStatic(System.String[]IPAddress, System.String[] SubnetMask)
Name : EnableStatic
IsInstance : True
答案 1 :(得分:0)
对于那些可能在搜索中偶然发现的人的另一个提示...
如果为您当前的IP地址分配了DHCP,并且EnableStatic()返回2147944122或2147944117错误代码,请确认Windows中的dhcp服务(DHCP客户端)已启用并启动。
我发现使用EnableStatic()从一个静态地址切换到另一个静态地址可以正常工作。但是,如果您具有动态地址,并且DHCP服务被禁用(可能是出于安全原因),则EnableStatic()将无法工作。
将此解决方案添加到Internet,以防将来节省时间。