时间:2021-05-20
本文实例实现了C#一键换IP、重置DNS、网关及掩码的功能,具体实现的功能为在程序界面窗口中设置ip地址和子网掩码,设置网关地址,设置DNS,并且在设置过程中程序将判断如果没有启用IP设置的网络设备则跳过,重置DNS为空,并开启DHCP。
主要功能代码如下:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Management;namespace changeIP{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ManagementBaseObject inPar = null; ManagementBaseObject outPar = null; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if (!(bool)mo["IPEnabled"]) continue; //设置ip地址和子网掩码 inPar = mo.GetMethodParameters("EnableStatic"); string ip = ""; ip = numericUpDown1.Value.ToString() + "." + numericUpDown2.Value.ToString() + "." + numericUpDown3.Value.ToString() + "." + numericUpDown4.Value.ToString(); inPar["IPAddress"] = new string[] { ip };// 1.备用 2.IP string ym = ""; ym = numericUpDown8.Value.ToString() + "." + numericUpDown7.Value.ToString() + "." + numericUpDown6.Value.ToString() + "." + numericUpDown5.Value.ToString(); inPar["SubnetMask"] = new string[] { ym }; outPar = mo.InvokeMethod("EnableStatic", inPar, null); //设置网关地址 inPar = mo.GetMethodParameters("SetGateways"); string wg = ""; wg = numericUpDown12.Value.ToString() + "." + numericUpDown11.Value.ToString() + "." + numericUpDown10.Value.ToString() + "." + numericUpDown9.Value.ToString(); inPar["DefaultIPGateway"] = new string[] { wg }; // 1.网关;2.备用网关 outPar = mo.InvokeMethod("SetGateways", inPar, null); //设置DNS inPar = mo.GetMethodParameters("SetDNSServerSearchOrder"); string dns1 = numericUpDown16.Value.ToString() + "." + numericUpDown15.Value.ToString() + "." + numericUpDown14.Value.ToString() + "." + numericUpDown13.Value.ToString(); string dns2 = numericUpDown20.Value.ToString() + "." + numericUpDown19.Value.ToString() + "." + numericUpDown18.Value.ToString() + "." + numericUpDown17.Value.ToString(); inPar["DNSServerSearchOrder"] = new string[] { dns1, dns2 }; // 1.DNS 2.备用DNS outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null); break; } } private void button2_Click(object sender, EventArgs e) { 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); } } private void button3_Click(object sender, EventArgs e) { this.Close(); this.Dispose(); } private void Form1_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.F2: button1_Click(sender, e); break; case Keys.F3: button2_Click(sender, e); break; } } }}声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文以一个完整实例形式介绍了C++实现获取IP、子网掩码、网关、DNS等本机网络参数的方法,供大家参考,具体的完整实例如下:#pragmacomment(lib
说明:ip:192.168.21.172子网掩码:255.255.255.0网关:192.168.21.2dns:8.8.8.88.8.4.41、设置ip地址v
说明:IP:192.168.21.173子网掩码:255.255.255.0网关:192.168.21.2DNS:8.8.8.88.8.4.41、设置IP地址、
本文实例讲述了C#修改及重置电脑密码DirectoryEntry实现方法。分享给大家供大家参考。具体如下:C#修改电脑密码方法如下://////更改Window
1、设置网卡IP地址为:10.1.1.2;子网掩码:255.0.0.0;网关:10.1.1.1;DNS:202.99.96.68(Win9X重启,Win2K/X