c# - Problems using the WMI EnableStatic method -


i'm trying create tool converts dynamic dhcp-provided ipv4 address, gateway , dns-settings static configuration. i've tried use wmi solve puzzle, have problem can't figure out.

the application completes, dns , gateway configured, enablestatic method (to set ip address , subnet) has been unsuccesful means ip still received dhcp (with greyed out fields) though default gateway has been set. how fix this?

the returnvalue enablestatic 70 (invalid ip address). weird thing input parameters same extracted nic 2 seconds earlier.

here code (except 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 static nic settings                 if (!setnicstatic())                 {                     messagebox.show("setting nic settings 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 * win32_networkadapterconfiguration ipenabled = 'true'");             querycollection = query.get();             //make nic string array             int = querycollection.count;             networkinterfaces = new string[i];             //fill nic string array             = 0;             foreach (managementobject mo in querycollection)             {                 networkinterfaces[i] = (string)mo["description"];                 i++;             }             //fill dropbox arraylist-data             nicselecter.datasource = networkinterfaces;         }          private boolean getnicsettings()         {             //get selected nic             int = 0;             foreach (managementobject mo in querycollection)             {                 //get settings 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 = 0;             foreach (managementobject mo in querycollection)             {                 //get settings specific nic                 if (i == currentnic)                 {                     try                     {                         //set static ip , 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?

could inputting ipv6 addresses well? playing around powershell seems not them. perhaps can post actual values being inputted whilst debugging, lot. maybe try statically inputting values like:

new string[]{"192.168.0.1"}, new string[] {"255.255.255.255"} 

also unless really, need c# , gui may want consider using powershell (requirement being installed of course) wmi simpler manipulate there (sadly still have learning curve though).

this example of how use powershell, can @ least use testing:

get-wmiobject win32_networkadapterconfiguration 

then index of adapter run, replace index number:

$obj = get-wmiobject win32_networkadapterconfiguration | {$_.index -eq 1} $obj.enablestatic("192.168.0.1", "255.255.255.0") 

to method parameters run:

$obj.enablestatic 

it return:

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 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -