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; } } }
上面的网络设置SetDHCPAddress()方法有些bug,一时找不到原因,下面用启动cmd.exe程序来更改网络配置
public void SetDHCPAddress(string ip, string submask, string getway, string dns,bool getself) { //string output = string.Empty; Process pro = new Process(); pro.StartInfo.FileName = "cmd.exe"; pro.StartInfo.UseShellExecute = false; //不使用系统外壳驱动程序 pro.StartInfo.RedirectStandardInput = true;//可以重定向输入 pro.StartInfo.RedirectStandardOutput = true; pro.StartInfo.RedirectStandardError = true; pro.StartInfo.CreateNoWindow = true; //不创建创窗口 pro.Start(); //不自动获取 if (!getself) { pro.StandardInput.WriteLine($"netsh interface ip set address "本地连接" static {ip} {submask} {getway} 1"); pro.StandardInput.WriteLine($"netsh interface ip set dns "本地连接" source=static addr={dns}"); //output = pro.StandardOutput.ReadToEnd(); } else { //自动获取 pro.StandardInput.WriteLine($"netsh interface ip set address name="本地连接" source=dhcp"); pro.StandardInput.WriteLine($"netsh interface ip set dns name="本地连接" source=dhcp"); } System.Threading.Thread.Sleep(6000); pro.Close(); //第二种只启动一条命令的 //ProcessStartInfo procInfo = new ProcessStartInfo(); //procInfo.FileName = "cmd.exe"; //procInfo.Arguments = $"/netsh interface ip set address "本地连接" static {ip} {submask} {getway} 1 "; //procInfo.CreateNoWindow = true; //Process prox = new Process(); //prox= Process.Start(procInfo); //prox.WaitForExit(); //if (pro.ExitCode==0) //{ // message = "修改成功"; //} //else if (pro.ExitCode==-1) //{ // message = "出现问题,修改失败"; //} }