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" of this management class. Here has basically included the Ip,dns, gateway settings information.
Using WMI in C # is relatively straightforward:
Copy Code code 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, setting various parameters of the network (DNS, gateway, subnet mask, IP)
</summary>
public class Networksetting
{
Public networksetting ()
{
Constructor logic
}
<summary>
Setting up DNS
</summary>
<param name= "DNS" ></param>
public static void Setdns (string[] DNS)
{
Setipaddress (NULL, NULL, NULL, DNS);
}
<summary>
Setting up Gateways
</summary>
<param name= "Getway" ></param>
public static void Setgetway (String getway)
{
Setipaddress (NULL, NULL, new string[] {getway}, NULL);
}
<summary>
Setting up Gateways
</summary>
<param name= "Getway" ></param>
public static void Setgetway (string[] getway)
{
Setipaddress (null, NULL, getway, NULL);
}
<summary>
Set 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>
Setting IP addresses, masks, and gateways
</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[] getway, string[] DNS)
{
ManagementClass WMI = new ManagementClass ("Win32_NetworkAdapterConfiguration");
Managementobjectcollection MoC = WMI. GetInstances ();
Managementbaseobject inpar = null;
Managementbaseobject outpar = null;
foreach (ManagementObject mo in MOC)
{
If a 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 = mo. Getmethodparameters ("SetGateways");
inpar["DefaultIPGateway"] = Getway;
Outpar = mo. InvokeMethod ("SetGateways", inpar, NULL);
}
Setting up DNS Addresses
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 (ManagementObject mo in MOC)
{
If a network device is not enabled for IP settings, skip
if (!) ( BOOL) mo["ipenabled"])
Continue
Reset DNS to NULL
Mo. InvokeMethod ("SetDNSServerSearchOrder", null);
Turn on DHCP
Mo. InvokeMethod ("EnableDHCP", null);
}
}
<summary>
Determine if the IP address format is compliant
</summary>
<param name= "IP" ></param>
<returns></returns>
public static bool Isipaddress (string IP)
{
The full IP is "." Grouping for boundaries
string[] arr = IP. Split ('. ');
Determine if IP is composed of four groups
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 starts at 0
if (i = = 0 && D = = "0")
return false;
Determine if IP is made up of 1~3 digits
if (! Regex.IsMatch (d, pattern))
return false;
if (d!= "0")
{
Determine whether each group of IP is all 0
D = D.trimstart (' 0 ');
if (d = = "")
return false;
Determine if IP is greater than 255 per group
if (int. Parse (d) > 255)
return false;
}
return true;
}
}
}
Well, after writing the above class, just wait for where it needs to be and the new one will be fine. Quite simply, if you encounter a setup failure, perhaps because of insufficient permissions, please refer to C # By default to run the program as an administrator