VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C#教程之C#实现一键换IP、重置DNS、网关及掩码的

本文实例实现了C#一键换IP、重置DNS、网关及掩码的功能,具体实现的功能为在程序界面窗口中设置ip地址和子网掩码,设置网关地址,设置DNS,并且在设置过程中程序将判断如果没有启用IP设置的网络设备则跳过,重置DNS为空,并开启DHCP。

主要功能代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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;
      }
    }
  }
}


相关教程