C#设置本地网络(DNS、网关、子网掩码、IP)

时间:2022-05-26 02:13:42

如今网络在我们的生活事情中所起的感化越来越大,可以说分开了网络我们就无法正常的事情和生活。作为措施员我们写的措施大大都也会跟网络相关,而想要使用网络首先要将机器的网络配置设置好。而手动设置的要领显然很不成取,所以我们要让措施帮我们完成。下面是一个很常用的C#设置系统各类网络参数的一个小Demo一起看看吧。


这个Demo是通过"Win32_NetworkAdapterConfiguration"这个打点类.这里面已根基包孕了IP,DNS,网关的设置信息。


在C#中使用WMI还是对照简单的:

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> /// 网络设置类,设置网络的各类参数(DNS、网关、子网掩码、IP) /// </summary> public class NetworkSetting { public NetworkSetting() { // 结构函数逻辑 } /// <summary> /// 设置DNS /// </summary> /// <param></param> public static void SetDNS(string[] dns) { SetIPAddress(null, null, null, dns); } /// <summary> /// 设置网关 /// </summary> /// <param></param> public static void SetGetWay(string getway) { SetIPAddress(null, null, new string[] { getway }, null); } /// <summary> /// 设置网关 /// </summary> /// <param></param> public static void SetGetWay(string[] getway) { SetIPAddress(null, null, getway, null); } /// <summary> /// 设置IP地点和掩码 /// </summary> /// <param></param> /// <param></param> public static void SetIPAddress(string ip, string submask) { SetIPAddress(new string[] { ip }, new string[] { submask }, null, null); } /// <summary> /// 设置IP地点,掩码和网关 /// </summary> /// <param></param> /// <param></param> /// <param></param> public static void SetIPAddress(string ip, string submask, string getway) { SetIPAddress(new string[] { ip }, new string[] { submask }, new string[] { getway }, null); } /// <summary> /// 设置IP地点,掩码,网关和DNS /// </summary> /// <param></param> /// <param></param> /// <param></param> /// <param></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) { //如果没有启用IP设置的网络设备则跳过 if (!(bool) mo["IPEnabled"]) continue; //设置IP地点和掩码 if (ip != null && submask != null) { inPar = mo.GetMethodParameters("EnableStatic"); inPar["IPAddress"] = ip; inPar["SubnetMask"] = submask; outPar = mo.InvokeMethod("EnableStatic", inPar, null); } //设置网关地点 if (getway != null) { inPar = mo.GetMethodParameters("SetGateways"); inPar["DefaultIPGateway"] = getway; outPar = mo.InvokeMethod("SetGateways", inPar, null); } //设置DNS地点 if (dns != null) { inPar = mo.GetMethodParameters("SetDNSServerSearchOrder"); inPar["DNSServerSearchOrder"] = dns; outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null); } } } /// <summary> /// 启用DHCP处事器 /// </summary> public static void EnableDHCP() { ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = wmi.GetInstances(); foreach (ManagementObject mo in moc) { //如果没有启用IP设置的网络设备则跳过 if (!(bool) mo["IPEnabled"]) continue; //重置DNS为空 mo.InvokeMethod("SetDNSServerSearchOrder", null); //开启DHCP mo.InvokeMethod("EnableDHCP", null); } } /// <summary> /// 判断是否切合IP地点格局 /// </summary> /// <param></param> /// <returns></returns> public static bool IsIPAddress(string ip) { //将完整的IP以“.”为边界分组 string[] arr = ip.Split(‘.‘); //判断IP是否为四组数构成 if (arr.Length != 4) return false; //正则表达式,1~3位整数 string pattern = @"\d{1,3}"; for (int i = 0; i < arr.Length; i++) { string d = arr[i]; //判断IP开头是否为0 if (i == 0 && d == "0") return false; //判断IP是否是由1~3位数构成 if (!Regex.IsMatch(d, pattern)) return false; if (d != "0") { //判断IP的每组数是否全为0 d = d.TrimStart(‘0‘); if (d == "") return false; //判断IP每组数是否大于255 if (int.Parse(d) > 255) return false; } } return true; } } }