• 用C#实现对本机IP地址的设置


        部分代码来自网络,自己收集整理写出来的这个小程序可以对本地IP地址的设置,源码公开!因为IP设置程序程序需要使用到WMI,因此首先添加对WMI的引用,打开【解决方案资源管理器】,在【引用】项上单击右键,并选择【添加引用】菜单项,有需要的可以拿用,程序界面如图:

    这里用到一类MaskedTextBox控件,具体使用大家可以百度一下,这里便不多说。源代码如下:

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Linq;
      7 using System.Text;
      8 using System.Windows.Forms;
      9 using System.Management;
     10 using System.IO;
     11 
     12 namespace IPSet
     13 {
     14     public partial class FrmIPset : Form
     15     {
     16         public FrmIPset()
     17         {
     18             InitializeComponent();
     19             // 获取本地计算机所有网卡信息
     20             string carName = "";
     21             ManagementObjectSearcher search = new ManagementObjectSearcher("SELECT * FROM Win32_NetWorkAdapterConfiguration");
     22             foreach (ManagementObject sear in search.Get())
     23             {
     24                 if (sear["IPAddress"] != null)
     25                 {
     26                     carName = sear["Description"].ToString().Trim();
     27                     comboBox1.Items.Add(carName);
     28                 }
     29             }
     30             comboBox1.SelectedIndex = 0;
     31         }
     32         private void button1_Click(object sender, EventArgs e)
     33         {
     34             ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
     35             ManagementObjectCollection moc = mc.GetInstances();
     36             foreach (ManagementObject mo in moc)
     37             {
     38                 if ((bool)mo["IPEnabled"])
     39                 {
     40                     if (mo["Description"].ToString() == comboBox1.SelectedItem.ToString().Trim())
     41                     {
     42                         ManagementBaseObject newIP =mo.GetMethodParameters("EnableStatic");
     43                         ManagementBaseObject newGateway =mo.GetMethodParameters("SetGateways");
     44                         ManagementBaseObject newDNS =mo.GetMethodParameters("SetDNSServerSearchOrder");
     45                         // 将要修改的目标 IP 地址
     46                      //   string selectNewIP;
     47                         string IPStr = "";
     48                         string[] IPPart = maskedTextBox1.Text.Split('.');
     49                         for (int i = 0; i <= 3;i++ )
     50                         {
     51                             //删除尾部首部的空格
     52                             IPStr+=(IPPart[i].Trim()+".");
     53                         }
     54                         IPStr = IPStr.Substring(0,IPStr.Length-1);
     55                         newIP["IPAddress"] = new string[] { IPStr };
     56 
     57                         //设置子网掩码
     58                          string subStr = "";
     59                          string[] subPart = mtxtSubNetwork.Text.Split('.');
     60                          for (int i = 0; i <= 3; i++)
     61                          {
     62                              //删除尾部首部的空格
     63                              subStr += (subPart[i].Trim() + ".");
     64                          }
     65                          subStr = subStr.Substring(0, subStr.Length - 1);
     66                          newIP["SubnetMask"] = new string[] { subStr };
     67 
     68                          //设置网关地址 
     69                          string gatStr = "";
     70                          string[] gatPart = mtxtGateway.Text.Split('.');
     71                          for (int i = 0; i <= 3; i++)
     72                          {
     73                              gatStr += (gatPart[i].Trim() + ".");
     74                          }
     75                          gatStr = gatStr.Substring(0, gatStr.Length - 1);
     76                          newGateway["DefaultIPGateway"] = new string[] { gatStr };
     77                          // 将要修改的目标 DNS 首选地址
     78                          string dnsStr1 = "";
     79                          string[] dnsPart1 = mtxtDNS1.Text.Split('.');
     80                          for (int i = 0; i <= 3; i++)
     81                          {
     82                              dnsStr1 += (dnsPart1[i].Trim() + ".");
     83                          }
     84                          dnsStr1 = dnsStr1.Substring(0, dnsStr1.Length - 1);
     85                          // 将要修改的目标 DNS 备用地址
     86                          string dnsStr2 = "";
     87                          string[] dnsPart2 = mtxtDNS2.Text.Split('.');
     88                          for (int i = 0; i <= 3; i++)
     89                          {
     90                              dnsStr2 += (dnsPart2[i].Trim() + ".");
     91                          }
     92                          dnsStr2 = dnsStr2.Substring(0, dnsStr2.Length - 1);
     93                          newDNS["DNSServerSearchOrder"] = new string[] { dnsStr1, dnsStr2 };
     94                          // 修改网络设置
     95                          try
     96                          {
     97                              ManagementBaseObject setIP = mo.InvokeMethod("EnableStatic", newIP, null);
     98                              ManagementBaseObject setGateways = mo.InvokeMethod("SetGateways", newGateway, null);
     99                              ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);
    100                              MessageBox.Show("设置成功");
    101                          }
    102                          catch (Exception ex)
    103                          {
    104                              MessageBox.Show(ex .Message);
    105                          }
    106                          break;
    107                     }
    108                 }
    109             }
    110         }
    111 
    112         private void button2_Click(object sender, EventArgs e)
    113         {
    114             Application.Exit();
    115         }
    116         //使能修改按键
    117         private void maskedTextBox1_TextChanged(object sender, EventArgs e)
    118         {
    119             button1.Enabled = true;
    120         }
    121         //自动获取
    122         private void button3_Click(object sender, EventArgs e)
    123         {
    124             ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
    125             ManagementObjectCollection moc = mc.GetInstances();
    126             foreach (ManagementObject mo in moc)
    127             {
    128                 if ((bool)mo["IPEnabled"])
    129                 {
    130                     if (mo["Description"].ToString() == comboBox1.SelectedItem.ToString().Trim())
    131                     {
    132                         //重置DNS为空   
    133                         mo.InvokeMethod("SetDNSServerSearchOrder", null);
    134                         //开启DHCP   
    135                         mo.InvokeMethod("EnableDHCP", null);
    136                         MessageBox.Show("自动获取IP成功!");
    137                         break;
    138                     }
    139                 }
    140             }
    141         }
    142     }
    143 }
  • 相关阅读:
    软工网络15团队作业4——Alpha阶段敏捷冲刺
    (转) linux目录结构详细介绍
    ActiveMQ使用记录
    .NET4.5中WCF中默认生成的basicHttpsBinding的研究
    StackExchange.Redis的使用
    微信/QQ机器人的实现
    EntityFramework中的datetime2异常的解决
    在Web API中使用Swagger-UI开源组件(一个深坑的解决)
    (转)使用Migrations更新数据库结构(Code First )
    WebApi中帮助页Description的中文显示
  • 原文地址:https://www.cnblogs.com/dreamfactory/p/2709244.html
Copyright © 2020-2023  润新知