Nowadays, the Internet is playing an increasingly important role in our daily lives and work. It can be said that without the Internet, we will not be able to work and live normally. As a programmer, most of the programs we write will also be related to the network. To use the network, you must first set the network configuration of the machine. The manual setting method is obviously not desirable, so we need to let the program complete it for us. The following is a very common Demo of C # setting various network parameters of the system. This Demo is based on the "Win32_NetworkAdapterConfiguration" management class. The configuration information of IP, DNS, and gateway is basically included. It is relatively simple to use WMI in C #: [csharp] using System; using System. collections. generic; using System. text; using System. collections; using System. text; using System. management; using System. text. regularExpressions; namespace Demo {// <summary> // Network Setting class, set various network parameters (DNS, gateway, subnet mask, IP) /// </summary> public class NetworkSetting {public NetworkSetting () {// constructor logic} // <summary> /// set DNS /// </summa Ry >/// <param name = "dns"> </param> public static void SetDNS (string [] dns) {SetIPAddress (null, dns );} /// <summary> /// set the gateway /// </summary> /// <param name = "getway"> </param> public static void SetGetWay (string getway) {SetIPAddress (null, null, new string [] {getway}, null );} /// <summary> /// set the gateway /// </summary> /// <param name = "getway"> </param> public static void SetGetWay (string [] Getway) {SetIPAddress (null, null, getway, null );} /// <summary> /// set the ip address and mask /// </summary> /// <param name = "ip"> </param> /// <param name = "submask"> </param> public static void SetIPAddress (string ip, string submask) {SetIPAddress (new string [] {ip}, new string [] {submask}, null, null) ;}/// <summary> // set the ip address, mask and gateway /// </summary> /// <param name = "ip"> </param> /// <param name = "submask"> </Param> // <param name = "getway"> </param> public static void SetIPAddress (string ip, string submask, string getway) {SetIPAddress (new string [] {ip}, new string [] {submask}, new string [] {getway}, null );} /// <summary> /// set the IP address, mask, gateway and DNS /// </summary> /// <param name = "ip"> </param> /// <param name = "submask"> </param> /// <param name = "getway"> </param> /// <param name = "dns"> </param> public Static void SetIPAddress (string [] ip, string [] submask, string [] getway, string [] dns) {ManagementClass wmi = new ManagementClass ("Win32_NetworkAdapterConfiguration "); managementObjectCollection moc = wmi. getInstances (); ManagementBaseObject inPar = null; ManagementBaseObject outPar = null; foreach (ManagementObject mo in moc) {// skip if (! (Bool) mo ["IPEnabled"]) continue; // set the ip address and mask if (ip! = Null & submask! = Null) {inPar = mo. getMethodParameters ("EnableStatic"); inPar ["IPAddress"] = ip; inPar ["SubnetMask"] = submask; outPar = mo. invokeMethod ("EnableStatic", inPar, null);} // set the gateway address if (getway! = Null) {inPar = mo. getMethodParameters ("SetGateways"); inPar ["DefaultIPGateway"] = getway; outPar = mo. invokeMethod ("SetGateways", inPar, null);} // set the DNS address if (dns! = Null) {inPar = mo. getMethodParameters ("SetDNSServerSearchOrder"); inPar ["DNSServerSearchOrder"] = dns; outPar = mo. invokeMethod ("SetDNSServerSearchOrder", inPar, null) ;}}/// <summary> /// enable DHCP server // </summary> public static void EnableDHCP () {ManagementClass wmi = new ManagementClass ("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = wmi. getInstances (); foreach (Mana GementObject mo in moc) {// skip if (! (Bool) mo ["IPEnabled"]) continue; // reset DNS to null mo. invokeMethod ("SetDNSServerSearchOrder", null); // enable DHCP mo. invokeMethod ("EnableDHCP", null );}} /// <summary> /// determine whether the ip address format is correct /// </summary> /// <param name = "ip"> </param> // <returns> </returns> public static bool IsIPAddress (string ip) {// set the complete IP address to ". "is the boundary group string [] arr = ip. split ('. '); // determines whether the IP address is composed of four groups of if (arr. length! = 4) return false; // regular expression, 1 ~ Three-digit integer string pattern = @ "\ d {1, 3}"; for (int I = 0; I <arr. length; I ++) {string d = arr [I]; // determines whether the IP address starts with 0 if (I = 0 & d = "0 ") return false; // determine whether the IP address is from 1 ~ Make up if (! Regex. IsMatch (d, pattern) return false; if (d! = "0") {// determines whether the number of IP addresses in each group is all 0 d = d. trimStart ('0'); if (d = "") www.2cto.com return false; // judge whether the number of IP addresses in each group is greater than 255 if (int. parse (d)> 255) return false ;}} return true ;}}all right. After writing the class above, you can simply wait for a NEW one. It's easy. If the setting fails, it may be because of insufficient permissions. For details, refer to C # running the program as an administrator by default.