Manually setting up a local network is obviously undesirable, so we have to let the program help us to complete, the need for friends can refer to the following
Now the network in our life and work in the role of more and more, can be said to leave the network we will not be normal work and life. As a programmer, most of the programs we write are related to the network, and the network configuration of the machine should be set up in order to use the network first. The manual Setup method is obviously undesirable, so we have to let the program do it for us. Here is a very common C # Setup system a variety of network parameters of a small demo together look at it. This demo is through the "Win32_NetworkAdapterConfiguration" this management class. Here has basically included the Ip,dns, gateway settings information. Using WMI or simpler: code in C # is as follows: using system; using system.collections.generic; using system.text; using system; using system.collections; using system.text; using system.management; using system.text.regularexpressions; namespace demo { /// <summary> ///Network Settings class, set various parameters of the network (DNS, gateway, subnet mask, IP) ///</summary> public class networksetting { Public networksetting () { //constructor logic } ///<summary>& nbsp Set dns ///</summary> ///<param name= "DNS" ></param> public static void Setdns ( string[] DNS) { setipaddress (null, NULL, NULL, DNS); } <summary> ///Settings Gateway ///</summary> ///<param name= "Getway" ></param> public static void Setgetway (String getway) { setipaddress (null, NULL, new string[] {getway}, NULL); nbsp } ///<summary> ///settings Gateway ///</summary> ///<param name= "Getway" ></param > public static void Setgetway (string[] getway) { setipaddress (null, NULL, Getway, NULL); }&NB Sp <summary> ///set IP address and mask ///</summary> ///<param name= "IP" ></param> ///<param name= "Submask" ></param> public static void setipaddress (string IP, String submask) & nbsp { setipaddress (new string[] {IP}, new string[] {submask}, NULL, NULL); } <summary> / Set 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 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[] G Etway, string[] DNS { managementclass WMI = new ManagementClass ("Win32_NetworkAdapterConfiguration"); Managementobjectcollection MOC = WMI. GetInstances (); managementbaseobject inpar = null; managementbaseobject OutPar = null; foreach ( ManagementObject mo in MOC) { //////If network device is not enabled for IP settings, skip if (! BOOL) mo["ipenabled"]) continue; //Set 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 gateway address if (Getway!= null) { Inpar = m O.getmethodparameters ("SetGateways"); inpar["DefaultIPGateway"] = getway; outpar = mo. InvokeMethod ("SetGateways", Inpar, NULL); } //Set DNS address if (DNS!= null) { Inpar = MO.G Etmethodparameters ("SetDNSServerSearchOrder"); inpar["DNSServerSearchOrder"] = dns; OutPar = mo. InvokeMethod ("SetDNSServerSearchOrder", Inpar, null); } } } ///<summary> // /enable DHCP server ///</summary> public static void EnableDHCP () { managementclass WMI = new Manag Ementclass ("Win32_NetworkAdapterConfiguration"); managementobjectcollection MoC = WMI. GetInstances (); foreach (ManagementObject mo in MOC) { ///If network devices without IP settings are not enabled, skip if (! BOOL) mo["ipenabled"]) continue; //reset DNS to null mo. InvokeMethod ("SetDNSServerSearchOrder", NULL); //Open dhcp mo. InvokeMethod ("EnableDHCP", null); } } ///<summary> ///to determine whether the IP address format is compliant ///< /summary> ///<param name= "IP" ></param> ///<returns></returns> Public static bool Isipaddress (string IP) { //will complete IP with "." For boundary grouping string[] arr = IP. Split ('. '); //Determine if IP is four group number composed of if (arr). Length!= 4) return false; //Regular expression, 1~3 bit integer string pattern = @ ' d{1,3} '; for (int i = 0 ; I < arr. Length; i++) { string d = arr[i]; //Determine if IP start is 0 if (i = = 0 && D = = "0") return false; //Determine if IP is composed of 1~3 digits if (! Regex.IsMatch (d, pattern)) return false; IF(d!= "0") { //determining whether each group of IP is 0 d = D.trimstart (' 0 '); if (d = = "") return false; / /Determine whether IP is greater than 255 if (int) per group. Parse (d) > 255) return false; } } return true; } } } Well, when you've written the above class, you'll be waiting. And then the new one will be all you need. Quite simply, if you encounter a setup failure, it may be due to insufficient permissions, please refer to C # default Administrator run program