用C#实现对本机IP地址的设置
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
IP设置程序程序需要使用到WMI,因此首先添加对WMI的引用,打开【解决方案资源管理器】,在【引用】项上单击右键,并选择【添加引用】菜单项,有需要的可以拿用,程序界面如图:
这里用到一类MaskedTextBox控件,具体使用大家可以百度一下,这里便不多说。源代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Management; using System.IO;
namespace IPSet { public partial class FrmIPset : Form { public FrmIPset() { InitializeComponent(); // 获取本地计算机所有网卡信息 string carName = ""; ManagementObjectSearcher search = new ManagementObjectSearcher("SELECT * FROM Win32_NetWorkAdapterConfiguration"); foreach (ManagementObject sear in search.Get()) { if (sear["IPAddress"] != null) { carName = sear["Description"].ToString().Trim(); comboBox1.Items.Add(carName); } } comboBox1.SelectedIndex = 0; } private void button1_Click(object sender, EventArgs e) { ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"]) { if (mo["Description"].ToString() == comboBox1.SelectedItem.ToString().Trim()) { ManagementBaseObject newIP =mo.GetMethodParameters("EnableStatic"); ManagementBaseObject newGateway =mo.GetMethodParameters("SetGateways"); ManagementBaseObject newDNS =mo.GetMethodParameters("SetDNSServerSearchOrder"); // 将要修改的目标 IP 地址 // string selectNewIP; string IPStr = ""; string[] IPPart = maskedTextBox1.Text.Split('.'); for (int i = 0; i <= 3;i++ ) { //删除尾部首部的空格 IPStr+=(IPPart[i].Trim()+"."); } IPStr = IPStr.Substring(0,IPStr.Length-1); newIP["IPAddress"] = new string[] { IPStr };
//设置子网掩码 string subStr = ""; string[] subPart = mtxtSubNetwork.Text.Split('.'); for (int i = 0; i <= 3; i++) { //删除尾部首部的空格 subStr += (subPart[i].Trim() + "."); } subStr = subStr.Substring(0, subStr.Length - 1); newIP["SubnetMask"] = new string[] { subStr };
//设置网关地址 string gatStr = ""; string[] gatPart = mtxtGateway.Text.Split('.'); for (int i = 0; i <= 3; i++) { gatStr += (gatPart[i].Trim() + "."); } gatStr = gatStr.Substring(0, gatStr.Length - 1); newGateway["DefaultIPGateway"] = new string[] { gatStr }; // 将要修改的目标 DNS 首选地址 string dnsStr1 = ""; string[] dnsPart1 = mtxtDNS1.Text.Split('.'); for (int i = 0; i <= 3; i++) { dnsStr1 += (dnsPart1[i].Trim() + "."); } dnsStr1 = dnsStr1.Substring(0, dnsStr1.Length - 1); // 将要修改的目标 DNS 备用地址 string dnsStr2 = ""; string[] dnsPart2 = mtxtDNS2.Text.Split('.'); for (int i = 0; i <= 3; i++) { dnsStr2 += (dnsPart2[i].Trim() + "."); } dnsStr2 = dnsStr2.Substring(0, dnsStr2.Length - 1); newDNS["DNSServerSearchOrder"] = new string[] { dnsStr1, dnsStr2 }; // 修改网络设置 try { ManagementBaseObject setIP = mo.InvokeMethod("EnableStatic", newIP, null); ManagementBaseObject setGateways = mo.InvokeMethod("SetGateways", newGateway, null); ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null); MessageBox.Show("设置成功"); } catch (Exception ex) { MessageBox.Show(ex .Message); } break; } } } }
private void button2_Click(object sender, EventArgs e) { Application.Exit(); } //使能修改按键 private void maskedTextBox1_TextChanged(object sender, EventArgs e) { button1.Enabled = true; } //自动获取 private void button3_Click(object sender, EventArgs e) { ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"]) { if (mo["Description"].ToString() == comboBox1.SelectedItem.ToString().Trim()) { //重置DNS为空 mo.InvokeMethod("SetDNSServerSearchOrder", null); //开启DHCP mo.InvokeMethod("EnableDHCP", null); MessageBox.Show("自动获取IP成功!"); break; } } } } } } 该文章在 2021/5/21 9:46:47 编辑过 |
关键字查询
相关文章
正在查询... |