I am trying to create a tool that converts a dynamic DHCP IPv4 address, gateway and dns settings to a static configuration. I tried using WMI to solve this puzzle, but I have a problem that I cannot understand.
The application is completed, the DNS and the gateway are configured, but the EnableStatic method (for setting the IP address and subnet) was unsatisfactory, which means that the IP address is still obtained from DHCP (with the fields highlighted), although the default gateway is set. How to fix it?
The return value from EnableStatic is 70 (invalid IP address). The strange thing is that the input parameters are the same that I extracted from the NIC 2 seconds earlier.
Here is the code (except for the 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 }
Any ideas?
c # wmi
Frode F.
source share