前言:工作时经常需要对笔记本电脑的 IP 进行切换,为了实现快速切换 IP,就写了这个小程序。
说明:
1、实现 IP 查询;
2、可配置参数文件;
3、主动写入 IP 地址,子网掩码,网关;
4、更改自动获取 IP。
说明:
1、实现 IP 查询;
2、可配置参数文件;
3、主动写入 IP 地址,子网掩码,网关;
4、更改自动获取 IP。
二、源代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Management; using System.Threading; using System.Configuration; namespace Change_IP { public partial class change_ip : Form { public change_ip() { InitializeComponent(); string ip1_name = ConfigurationManager.AppSettings["IP1_Name"]; string ip2_name = ConfigurationManager.AppSettings["IP2_Name"]; string ip3_name = ConfigurationManager.AppSettings["IP3_Name"]; this.button1.Text = ip1_name; this.button2.Text = ip2_name; this.button3.Text = ip3_name; } private void button1_Click(object sender, EventArgs e) { string ip = ConfigurationManager.AppSettings["IP1"]; string mask = ConfigurationManager.AppSettings["IP1_MASK"]; string getway = ConfigurationManager.AppSettings["IP1_GETWAY"]; setting_ip(ip, mask, getway); string name = ConfigurationManager.AppSettings["IP1_Name"]; logs_out(name + "设置完成。"); } private void button4_Click(object sender, EventArgs e) { ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = wmi.GetInstances(); //ManagementBaseObject outPar = null; foreach (ManagementObject mo in moc) { if (!(bool)mo["IPEnabled"]) continue; mo.InvokeMethod("SetDNSServerSearchOrder", null); mo.InvokeMethod("EnableDHCP", null); } logs_out("自动获取 IP 设置完成。"); } private void button5_Click(object sender, EventArgs e) { string name = Dns.GetHostName(); IPAddress[] ipadrlist = Dns.GetHostAddresses(name); foreach (IPAddress ipa in ipadrlist) { if (ipa.AddressFamily == AddressFamily.InterNetwork) { logs_out(ipa.ToString()); } else { continue; //this.list_logs.Items.Add(ipa.ToString()); } } } private void setting_ip(string ip,string mask,string getway) { ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = wmi.GetInstances(); ManagementBaseObject inPar = null; ManagementBaseObject outPar = null; foreach (ManagementObject mo in moc) { if (!(bool)mo["IPEnabled"]) continue; //设置 IP 地址和子网掩码 inPar = mo.GetMethodParameters("EnableStatic"); inPar["IPAddress"] = new string[] { ip }; System.Console.Out.WriteLine(inPar["IPAddress"].ToString()); inPar["SubnetMask"] = new string[] { mask }; System.Console.Out.WriteLine(inPar["SubnetMask"].ToString()); outPar = mo.InvokeMethod("EnableStatic", inPar, null); //设置网关地址 inPar = mo.GetMethodParameters("SetGateways"); inPar["DefaultIPGateway"] = new string[] { getway }; //inPar["DefaultIPGateway"] = new string[] { "10.4.250.254" }; outPar = mo.InvokeMethod("SetGateways", inPar, null); //设置 DNS //inPar = mo.GetMethodParameters("SetDNSServerSearchOrder"); //inPar["DNSServerSearchOrder"] = new string[] { "10.4.246.1", "8.8.8.8" }; // 1.DNS 2.备用 DNS //outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null); break; } } private void button2_Click(object sender, EventArgs e) { string ip = ConfigurationManager.AppSettings["IP2"]; string mask = ConfigurationManager.AppSettings["IP2_MASK"]; string getway = ConfigurationManager.AppSettings["IP2_GETWAY"]; setting_ip(ip, mask, getway); string name = ConfigurationManager.AppSettings["IP2_Name"]; logs_out(name + "设置完成。"); } private void button3_Click(object sender, EventArgs e) { string ip = ConfigurationManager.AppSettings["IP3"]; string mask = ConfigurationManager.AppSettings["IP3_MASK"]; string getway = ConfigurationManager.AppSettings["IP3_GETWAY"]; setting_ip(ip, mask, getway); string name = ConfigurationManager.AppSettings["IP3_Name"]; logs_out(name+"设置完成。"); } private void logs_out(string logs) { string now_time = DateTime.Now.ToLocalTime().ToString("hh:mm:ss"); this.list_logs.Items.Insert(0,(now_time+"==>"+logs)); } private void label1_Click(object sender, EventArgs e) { MessageBox.Show("版本号:2021.0227.1442", "版本说明"); } } }
三、Config 配置文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" /> </startup> <appSettings> <add key="IP1_Name" value="监狱管理局"></add> <add key="IP1" value="10.1.115.166"></add> <add key="IP1_MASK" value="255.255.255.0"></add> <add key="IP1_GETWAY" value="10.1.115.254"></add> <add key="IP2_Name" value="未管所"></add> <add key="IP2" value="10.16.53.37"></add> <add key="IP2_MASK" value="255.255.255.0"></add> <add key="IP2_GETWAY" value="10.6.53.254"></add> <add key="IP3_Name" value="省一监"></add> <add key="IP3" value="10.2.107.166"></add> <add key="IP3_MASK" value="255.255.255.0"></add> <add key="IP3_GETWAY" value="10.2.107.254"></add> </appSettings> </configuration>
四、文件下载地址
百度云提取码: 545p
百度云提取码: 545p