It has been found that there is no need for a long time, and it is inevitable that some have a good job in these days, just write something. A lot of problems with Notebook's friends, the company and the network settings at home, so I want to change the network settings of the NIC when I go home and return home. I am also more annoying this thing. It is very troublesome to change every day. Today, I decide to write a small program to simplify this step. Checked the DOTNET related things and articles, and found it. Using System.Management This stuff is encapsulated. The settings of the NIC are not as four, IP addresses, subnet masks, gateways, DNS set up four string variables: private string ipaddress; private string netgate; PRIVATE STRING DNS;
private string IpAddree {set {ipaddress = value;} get {return ipaddress;}} private string SubCode {set {subcode = value;} get {return subcode;}} private string NetGate {set {netgate = value;} get {return Netgate;}}
private string Dns {set {dns = value;} get {return dns;}} using the following method to change the card settings: private void changeip () {ManagementBaseObject inPar = null; ManagementBaseObject outPar = null; ManagementClass mc = new ManagementClass ( " Win32_networkadapterconfiguration "); managementObjectCollection moc = mc.getinstances (); // Traverse all network cards in the working status
Foreach (ManagementObject Mo In MoC) {IF (! (bool) mo ["ipenabled"]) Continue;
// set ipaddress & subnetMask INPAR = mo.getMethodParameters ("ipaddress"); inPar [ipaddress "] = new string [ipaddree}; inPar [" subnetMask "] = new string [] {subcode}; outpar = mo .Invokemethod ("enablestatic", inPar, NULL;
// set getway inPar = mo.getMethodParameters ("setgateways"); inPar ["defaultgateway"] = new string [] {netgate}; outpar = mo.invokeMethod ("setgateways", inPar, null); // set DNS INPAR = mo.GetMethodParameters ( "SetDNSServerSearchOrder"); inPar [ "DNSServerSearchOrder"] = new string [] {Dns}; outPar = mo.InvokeMethod ( "SetDNSServerSearchOrder", inPar, null);}} is so simple, the above approach To check the relevant things in the MSDN library, most of the network cards can be changed by the .NET program. Below is all code: use system; use system.drawing; use system.collections; using system.componentmodel; using system.windows.forms; use system.data; using system.management;
Summary description of Namespace ipadd {///
Private string ipaddress; private string netgate; private string dns;
private string IpAddree {set {ipaddress = value;} get {return ipaddress;}} private string SubCode {set {subcode = value;} get {return subcode;}} private string NetGate {set {netgate = value;} get {return Netgate;}}
PRIVATE STRING DNS {set {DNS = Value;} Get {Return DNS;}}
Public Form1 () {// // Windows Form Designer Support for // InitializeComponent ();
// // Todo: Add any constructor code after INITIALIZEComponent call //}
///
} #Endregion
///
Private void button1_click (object sender, system.eventargs e) {if (cbbaddress.text.tostring ()! = "") {this.ipaddree = cbbaddress.text.toString (); if (iPaddree == "192.168.1.9" ) {This.Subcode = "THISNETGATE =" 192.168.1.1 "; this.dns =" 202.106.0.20 ";} else if (iPadDree ==" 192.168.6.14 ") {this.subcode = "255.255.255.0"; this.Netgate = "192.168.6.250"; this.dns = "192.168.0.10";}} Changeip ();
Messagebox.show ("IP address change");
private void changeip () {ManagementBaseObject inPar = null; ManagementBaseObject outPar = null; ManagementClass mc = new ManagementClass ( "Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances ();
Foreach (ManagementObject Mo In MoC) {IF (! (bool) mo ["ipenabled"]) Continue;
// set ipaddress & subnetMask INPAR = mo.getMethodParameters ("ipaddress"); inPar [ipaddress "] = new string [ipaddree}; inPar [" subnetMask "] = new string [] {subcode}; outpar = mo .Invokemethod ("enablestatic", inPar, NULL;
// set getway INPAR = Mo.getMethodParameters; INPAR ["defaultgateway"] = new string [] {netgate}; outpar = mo.invokemethod ("setgateways", inPar, NULL);
// set dns inPar = mo.GetMethodParameters ( "SetDNSServerSearchOrder"); inPar [ "DNSServerSearchOrder"] = new string [] {Dns}; outPar = mo.InvokeMethod ( "SetDNSServerSearchOrder", inPar, null);}}