用C#實(shí)現(xiàn)對(duì)本機(jī)IP地址的設(shè)置
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
IP設(shè)置程序程序需要使用到WMI,因此首先添加對(duì)WMI的引用,打開(kāi)【解決方案資源管理器】,在【引用】項(xiàng)上單擊右鍵,并選擇【添加引用】菜單項(xiàng),有需要的可以拿用,程序界面如圖:
這里用到一類(lèi)MaskedTextBox控件,具體使用大家可以百度一下,這里便不多說(shuō)。源代碼如下: 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(); // 獲取本地計(jì)算機(jī)所有網(wǎng)卡信息 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"); // 將要修改的目標(biāo) 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 };
//設(shè)置子網(wǎng)掩碼 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 };
//設(shè)置網(wǎng)關(guān)地址 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 }; // 將要修改的目標(biāo) 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); // 將要修改的目標(biāo) 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 }; // 修改網(wǎng)絡(luò)設(shè)置 try { ManagementBaseObject setIP = mo.InvokeMethod("EnableStatic", newIP, null); ManagementBaseObject setGateways = mo.InvokeMethod("SetGateways", newGateway, null); ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null); MessageBox.Show("設(shè)置成功"); } 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; } //自動(dòng)獲取 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); //開(kāi)啟DHCP mo.InvokeMethod("EnableDHCP", null); MessageBox.Show("自動(dòng)獲取IP成功!"); break; } } } } } } 該文章在 2021/5/21 9:46:47 編輯過(guò) |
關(guān)鍵字查詢(xún)
相關(guān)文章
正在查詢(xún)... |