• IP配置程序的设计与实现


      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.Threading.Tasks;
      9 using System.Windows.Forms;
     10 
     11 using System.Management;
     12 using System.IO;
     13 using System.Net.NetworkInformation;
     14 using System.Net;
     15 
     16 namespace Ip
     17 {
     18     public partial class Form1 : Form
     19     {
     20         public Form1()
     21         {
     22             InitializeComponent();
     23             // 获取本地计算机所有网卡信息
     24             string carName = "";
     25             ManagementObjectSearcher search = new ManagementObjectSearcher("SELECT * FROM Win32_NetWorkAdapterConfiguration");
     26             foreach (ManagementObject sear in search.Get())
     27             {
     28                 if (sear["IPAddress"] != null)
     29                 {
     30                     carName = sear["Description"].ToString().Trim();
     31                     comboBox1.Items.Add(carName);
     32                 }
     33             }
     34             comboBox1.SelectedIndex = 0;
     35         }
     36 
     37         private void button1_Click(object sender, EventArgs e)
     38         {
     39 
     40             ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
     41             ManagementObjectCollection moc = mc.GetInstances();
     42             foreach (ManagementObject mo in moc)
     43             {
     44 
     45                 if ((bool)mo["IPEnabled"])
     46                 {
     47 
     48                   if (mo["Description"].ToString() == comboBox1.SelectedItem.ToString().Trim() )
     49                     {
     50 
     51                         //获取IP
     52                         string IpAddress = "";
     53                         System.Array ar;
     54                         ar = (System.Array)(mo.Properties["IpAddress"].Value);
     55                         IpAddress = ar.GetValue(0).ToString();
     56                         textBox1.Text = IpAddress;
     57                         //获取子网掩码
     58                         string IPSubnet = "";
     59                         ar = (System.Array)(mo.Properties["IPSubnet"].Value);
     60                         IPSubnet = ar.GetValue(0).ToString();
     61                         textBox2.Text = IPSubnet;
     62 
     63                         //获取网关
     64                         string DefaultIPGateway = "";
     65                         ar = (System.Array)(mo.Properties["DefaultIPGateway"].Value);
     66                         if (ar != null)
     67                             DefaultIPGateway = ar.GetValue(0).ToString();
     68                         textBox3.Text = DefaultIPGateway;
     69                         //获取DNS
     70                         string DNSServerSearchOrder1 = "";
     71                         string DNSServerSearchOrder2 = "";
     72                         ar = (System.Array)(mo.Properties["DNSServerSearchOrder"].Value);
     73                         if (ar != null)
     74                         {
     75                             DNSServerSearchOrder1 = ar.GetValue(0).ToString();
     76                             DNSServerSearchOrder2 = ar.GetValue(1).ToString();
     77                         }
     78                         textBox4.Text = DNSServerSearchOrder1;
     79                         textBox5.Text = DNSServerSearchOrder2;
     80 
     81                         MessageBox.Show(comboBox1.SelectedItem.ToString() + "自动获取成功!");
     82 
     83                     }
     84 
     85 
     86                 }
     87             }
     88 
     89         }
     90 
     91         private void button2_Click(object sender, EventArgs e)
     92         {
     93             ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
     94             ManagementObjectCollection moc = mc.GetInstances();
     95             foreach (ManagementObject mo in moc)
     96             {
     97                 if ((bool)mo["IPEnabled"])
     98                 {
     99                     if (mo["Description"].ToString() == comboBox1.SelectedItem.ToString().Trim() )
    100                     {
    101                         ManagementBaseObject newIP = mo.GetMethodParameters("EnableStatic");
    102                         ManagementBaseObject newGateway = mo.GetMethodParameters("SetGateways");
    103                         ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder");
    104 
    105                         // 将要修改的目标 IP 地址
    106                         //   string selectNewIP;
    107                         string IPStr = "";
    108                         string[] IPPart = textBox1.Text.Split('.');
    109                        if(IPPart.Length == 4)
    110                         {
    111                             for (int i = 0; i <= 3; i++)
    112                             {
    113                                 //删除尾部首部的空格
    114                                 IPStr += (IPPart[i].Trim() + ".");
    115                             }
    116                             IPStr = IPStr.Substring(0, IPStr.Length - 1);
    117                         }
    118                         newIP["IPAddress"] = new string[] { IPStr };
    119 
    120                         //设置子网掩码
    121                         string subStr = "";
    122                         string[] subPart =textBox2.Text.Split('.');
    123                         if(subPart.Length == 4)
    124                         {
    125                             for (int i = 0; i <= 3; i++)
    126                             {
    127                                 //删除尾部首部的空格
    128                                 subStr += (subPart[i].Trim() + ".");
    129                             }
    130                             subStr = subStr.Substring(0, subStr.Length - 1);
    131                         }
    132                         
    133                         newIP["SubnetMask"] = new string[] { subStr };
    134 
    135                         //设置网关地址
    136                         string gatStr = "";
    137                         string[] gatPart = textBox3.Text.Split('.');
    138                         if(gatPart.Length == 4)
    139                         {
    140                             for (int i = 0; i <= 3; i++)
    141                             {
    142                                 gatStr += (gatPart[i].Trim() + ".");
    143                             }
    144                             gatStr = gatStr.Substring(0, gatStr.Length - 1);
    145                         }
    146                             
    147                         newGateway["DefaultIPGateway"] = new string[] { gatStr };
    148                         // 将要修改的目标 DNS 首选地址
    149                         string dnsStr1 = "";
    150                         string dnsStr2 = "";
    151                            
    152                         string[] dnsPart1 = textBox4.Text.Split('.');
    153                         if(dnsPart1.Length == 4)
    154                         {
    155                             for (int i = 0; i <= 3; i++)
    156                             {
    157                                 dnsStr1 += (dnsPart1[i].Trim() + ".");
    158                             }
    159                             dnsStr1 = dnsStr1.Substring(0, dnsStr1.Length - 1);
    160                         }
    161                         // 将要修改的目标 DNS 备用地址      
    162                         string[] dnsPart2 = textBox5.Text.Split('.');
    163                         if( dnsPart2.Length == 4)
    164                         {
    165                             for (int i = 0; i <= 3; i++)
    166                             {
    167                                 dnsStr2 += (dnsPart2[i].Trim() + ".");
    168                             }
    169                             dnsStr2 = dnsStr2.Substring(0, dnsStr2.Length - 1);
    170                         }
    171                       
    172                         newDNS["DNSServerSearchOrder"] = new string[] { dnsStr1, dnsStr2 };
    173 
    174                         // 修改网络设置
    175                         try
    176                         {
    177                             ManagementBaseObject setIP = mo.InvokeMethod("EnableStatic", newIP, null);
    178                             ManagementBaseObject setGateways = mo.InvokeMethod("SetGateways", newGateway, null);
    179                             ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);
    180 
    181                             MessageBox.Show("设置成功");
    182                         }
    183                         catch (Exception ex)
    184                         {
    185                             MessageBox.Show(ex.Message);
    186                         }
    187                         break;
    188                     }
    189                 }
    190             }
    191         }
    192 
    193   
    194 
    195         private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
    196         {
    197             textBox1.Text = null;
    198             textBox2.Text = null;
    199             textBox3.Text = null;
    200             textBox4.Text = null;
    201             textBox5.Text = null;
    202         }
    203     }
    204 }
    View Code

     

  • 相关阅读:
    支付宝小程序InputItem清除icon不显示
    win11 激活 wi7 win11 魔兽争霸切换 后无法 回到游戏界面 处理办法
    HJ10 字符个数统计
    iOS 15系统导航栏适配
    HJ4 字符串分隔
    HJ7 取近似值
    [iOS]隐藏导航栏3种方式
    HJ3 明明的随机数
    HJ8 合并表记录
    HJ5 进制转换
  • 原文地址:https://www.cnblogs.com/hcl6/p/15767144.html
Copyright © 2020-2023  润新知