C#实现一键换IP、重置DNS、网关及掩码的方法
作者:shichen2014 时间:2021-10-03 00:32:33
本文实例实现了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;
}
}
}
}
标签:C#,换IP,DNS,网关,掩码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android ItemDecoration 实现分组索引列表的示例代码
2022-02-10 20:22:12
![](https://img.aspxhome.com/file/2023/1/98661_0s.gif)
浅谈Java自动装箱与拆箱及其陷阱
2023-03-22 23:09:23
![](https://img.aspxhome.com/file/2023/6/79856_0s.png)
java后端+前端使用WebSocket实现消息推送的详细流程
2022-04-24 12:34:53
![](https://img.aspxhome.com/file/2023/1/94371_0s.png)
Quarkus的Spring扩展快速改造Spring项目
2023-11-26 03:01:58
![](https://img.aspxhome.com/file/2023/2/60322_0s.png)
Solr通过特殊字符分词实现自定义分词器详解
2022-11-29 09:19:36
![](https://img.aspxhome.com/file/2023/6/91056_0s.png)
C#实现winform中RichTextBox在指定光标位置插入图片的方法
2022-04-04 01:50:16
java实现计算器功能
2022-10-28 06:52:09
![](https://img.aspxhome.com/file/2023/0/81980_0s.jpg)
Unity3d 使用Gizmos画一个圆圈
2022-05-22 05:10:52
![](https://img.aspxhome.com/file/2023/2/104332_0s.jpg)
C# 中用 Sqlparameter 的两种用法
2022-11-19 03:52:52
获取Android手机中所有短信的实现代码
2023-08-04 16:55:30
java静态工具类注入service出现NullPointerException异常处理
2021-11-28 09:59:11
![](https://img.aspxhome.com/file/2023/0/88520_0s.jpg)
Java利用Dijkstra算法求解拓扑关系最短路径
2021-08-24 10:30:54
![](https://img.aspxhome.com/file/2023/0/68510_0s.jpg)
Android语音声波控件 Android条形波控件
2023-10-29 02:03:05
OpenGL绘制三次Bezier曲线
2022-04-23 18:18:14
![](https://img.aspxhome.com/file/2023/3/112283_0s.jpg)
SpringBoot项目集成xxljob实现全纪录
2023-01-18 17:26:21
![](https://img.aspxhome.com/file/2023/4/86044_0s.png)
springboot 中整合mybatis多数据源不使用JPA
2023-03-01 08:43:02
![](https://img.aspxhome.com/file/2023/3/61823_0s.png)
详解Java中Collections.sort排序
2023-08-20 00:51:48
Java 数据流之Broadcast State
2022-05-21 15:17:19
![](https://img.aspxhome.com/file/2023/3/82983_0s.png)
JavaEE中struts2实现文件上传下载功能实例解析
2023-03-09 07:54:31
![](https://img.aspxhome.com/file/2023/7/88737_0s.png)
maven的生命周期及常用命令介绍
2022-03-10 17:21:16
![](https://img.aspxhome.com/file/2023/3/75443_0s.png)