C implements the method of changing IP with one key resetting DNS gateway and mask

  • 2020-06-23 01:46:28
  • OfStack

This instance realized C # 1 key change IP, reset DNS, gateway, and the function of the mask, the function of the specific implementation of the program interface window set ip address and subnet mask, set up the gateway address, set DNS, and in the process of setup program will judge if there is no skip enabling IP setting of network equipment, reset DNS is empty, and open DHCP.

The main function codes are as follows:


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;
        // Set up the ip Address and subnet mask 
        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. The standby  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);
        // Set the gateway address 
        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. The gateway ;2. Standby gateway 
        outPar = mo.InvokeMethod("SetGateways", inPar, null);
        // Set up the 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. The standby 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)
      {
        // If it's not enabled IP Network devices set are skipped 
        if (!(bool)mo["IPEnabled"])
          continue;
        // reset DNS Is empty 
        mo.InvokeMethod("SetDNSServerSearchOrder", null);
        // open 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;
      }
    }
  }
}

Related articles: