本地IP,掩码,网关,DNS设置
Posted Mr.zy丶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了本地IP,掩码,网关,DNS设置相关的知识,希望对你有一定的参考价值。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Management; using System.Net.NetworkInformation; using System.Text.RegularExpressions; namespace ZL.Shell业务.NetSetting { public class DHCPSetting { public string IpAddress { get; set; } //IP地址 public string SubMask { get; set; } //子网掩码 public string NetWay { get; set; } //网关 public string DNSAddress { get; set; } //DNS public static DHCPSetting _instance = null; private DHCPSetting() { } static DHCPSetting() { if (_instance == null) { _instance = new DHCPSetting(); } } public static DHCPSetting Instance { get { return _instance; } } /// <summary> /// 获取网络信息 /// </summary> public void GetDHCPAddress() { ManagementClass manger = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection manColl = manger.GetInstances(); foreach (ManagementObject item in manColl) { if (Convert.ToBoolean(item["ipEnabled"]) == true) { IpAddress = (item["IPAddress"] as string[])[0]; SubMask = (item["IPSubnet"] as string[])[0]; NetWay = (item["DefaultIPGateway"] as string[])[0]; break; } } NetworkInterface[] network = NetworkInterface.GetAllNetworkInterfaces(); foreach (var item in network) { IPAddressCollection dns = item.GetIPProperties().DnsAddresses; if (dns.Count > 0) { DNSAddress = dns[0].ToString(); } } } /// <summary> /// 设置网络信息 /// </summary> /// <param name="ip"></param> /// <param name="submask"></param> /// <param name="getway"></param> /// <param name="dns"></param> public void SetDHCPAddress(string ip, string submask, string getway, string dns) { ManagementClass manger = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection manColl = manger.GetInstances(); ManagementBaseObject inPar = null; ManagementBaseObject outPar = null; foreach (ManagementObject item in manColl) { //如果没有启用IP设置的网络设备则跳过 if (!(bool)item["IPEnabled"]) { continue; } //设置IP地址和掩码 if (ip != null && submask != null) { inPar = item.GetMethodParameters("EnableStatic"); inPar["IPAddress"] = new string[] { ip}; inPar["SubnetMask"] = new string[] { submask}; outPar = item.InvokeMethod("EnableStatic", inPar, null); } //设置网关地址 if (getway != null) { inPar = item.GetMethodParameters("SetGateways"); inPar["DefaultIPGateway"] = new string[] { getway }; outPar = item.InvokeMethod("SetGateways", inPar, null); } //设置DNS地址 if (dns != null) { inPar = item.GetMethodParameters("SetDNSServerSearchOrder"); inPar["DNSServerSearchOrder"] = new string[] { dns }; outPar = item.InvokeMethod("SetDNSServerSearchOrder", inPar, null); } } } /// <summary> /// 启用DHCP服务器 /// </summary> public void StartDHCP() { ManagementClass manger = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection manColl = manger.GetInstances(); foreach (ManagementObject item in manColl) { //如果没有启用IP设置的网络设备则跳过 if (!(bool)item["IPEnabled"]) continue; //重置DNS为空 item.InvokeMethod("SetDNSServerSearchOrder", null); //开启DHCP item.InvokeMethod("EnableDHCP", null); break; } } /// <summary> /// 判断输入格式是否正确 /// </summary> /// <param name="ip"></param> /// <returns></returns> public bool IsDHCPAddress(string ip) { string[] arr = ip.Split(‘.‘); if (arr.Length != 4) { return false; } //正则判断是否由1-3位数组成 foreach (var item in arr) { if (!Regex.IsMatch(item, @"\d{1,3}") || item == "" || int.Parse(item) > 255) { return false; } } return true; } } }
以上是关于本地IP,掩码,网关,DNS设置的主要内容,如果未能解决你的问题,请参考以下文章