程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#完成一鍵換IP、重置DNS、網關及掩碼的辦法

C#完成一鍵換IP、重置DNS、網關及掩碼的辦法

編輯:C#入門知識

C#完成一鍵換IP、重置DNS、網關及掩碼的辦法。本站提示廣大學習愛好者:(C#完成一鍵換IP、重置DNS、網關及掩碼的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成一鍵換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;
      }
    }
  }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved