C sets up local networks such as DNS gateways subnet masks IP and so on

  • 2020-11-25 07:13:38
  • OfStack

Nowadays the network is playing a more and more important role in our life and work. It can be said that we cannot work and live normally without the network. As programmers, most of the programs we write are also related to the network, and to use the network, we first need to set up the network configuration of the machine. Manual setup is obviously not an option, so we'll let the program do it for us. Here is a common C# to set the various network parameters of the system.

This Demo is managed through the "Win32_NetworkAdapterConfiguration" class. This has basically included IP,DNS, gateway Settings information.

Using WMI in C# is relatively simple:
 
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, which sets various parameters of the network ( DNS , gateway, subnet mask, IP )  
/// </summary> 
public class NetworkSetting 
{ 
public NetworkSetting() 
{ 
//  Constructor logic  
} 

/// <summary> 
///  Set up the DNS 
/// </summary> 
/// <param name="dns"></param> 
public static void SetDNS(string[] dns) 
{ 
SetIPAddress(null, null, 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 up 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 up 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 up 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) 
{ 
// If it's not enabled IP Network devices set are skipped  
if (!(bool) mo["IPEnabled"]) 
continue; 

// Set up 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 up the DNS address  
if (dns != null) 
{ 
inPar = mo.GetMethodParameters("SetDNSServerSearchOrder"); 
inPar["DNSServerSearchOrder"] = dns; 
outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null); 
} 
} 
} 

/// <summary> 
///  To enable the DHCP The server  
/// </summary> 
public static void EnableDHCP() 
{ 
ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration"); 
ManagementObjectCollection moc = wmi.GetInstances(); 
foreach (ManagementObject mo in moc) 
{ 
// If it's not enabled IP Network devices set are skipped  
if (!(bool) mo["IPEnabled"]) 
continue; 
// reset DNS Is empty  
mo.InvokeMethod("SetDNSServerSearchOrder", null); 
// open DHCP 
mo.InvokeMethod("EnableDHCP", null); 
} 
} 

/// <summary> 
///  Judge whether it fits IP Address format  
/// </summary> 
/// <param name="ip"></param> 
/// <returns></returns> 
public static bool IsIPAddress(string ip) 
{ 
// Will complete IP In order to" . "Is a grouping of boundaries  
string[] arr = ip.Split('.'); 


// judge IP Whether it is 4 Set of Numbers  
if (arr.Length != 4) 
return false; 


// Regular expressions, 1~3 An integer  
string pattern = @"\d{1,3}"; 
for (int i = 0; i < arr.Length; i++) 
{ 
string d = arr[i]; 


// judge IP Does it start with 0 
if (i == 0 && d == "0") 
return false; 


// judge IP Whether by 1~3 Digits of  
if (!Regex.IsMatch(d, pattern)) 
return false; 

if (d != "0") 
{ 
// judge IP Are all of the Numbers in each group 0 
d = d.TrimStart('0'); 
if (d == "") 
return false; 

// judge IP Whether the number of each group is greater than 255 
if (int.Parse(d) > 255) 
return false; 
} 
} return true; 
} 
} 
} 

Ok, so once you've got that class up there, you can wait until you need it and then NEW1 will be fine. Quite simply, if you encounter Settings that fail, perhaps because you don't have enough permissions, see C# to run the program as administrator by default

Related articles: