C#实现一键换IP、重置DNS、网关及掩码的方法
程序员文章站
2024-02-20 22:55:22
本文实例实现了c#一键换ip、重置dns、网关及掩码的功能,具体实现的功能为在程序界面窗口中设置ip地址和子网掩码,设置网关地址,设置dns,并且在设置过程中程序将判断如果...
本文实例实现了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; } } } }