码迷,mamicode.com
首页 > 其他好文 > 详细

修改IP地址

时间:2015-07-07 14:32:27      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

方法一:
1、在 “解决方案资源管理器” 右击 “引用” 添加 “System.Management” 的引用。
2、添加 “using System.Management;”。 
 1 static void SetNetworkAdapter()
 2         {
 3             ManagementBaseObject inPar = null;
 4             ManagementBaseObject outPar = null;
 5             ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
 6             ManagementObjectCollection moc = mc.GetInstances();
 7             foreach (ManagementObject mo in moc)
 8             {
 9                 if (!(bool)mo["IPEnabled"])
10                     continue;
11 
12                 //设置ip地址和子网掩码 
13                 inPar = mo.GetMethodParameters("EnableStatic");
14                 inPar["IPAddress"] = new string[] { "192.168.16.248", "192.168.16.249" };// 1.备用 2.IP
15                 inPar["SubnetMask"] = new string[] { "255.255.255.0", "255.255.255.0" };
16                 outPar = mo.InvokeMethod("EnableStatic", inPar, null);
17 
18                 //设置网关地址 
19                 inPar = mo.GetMethodParameters("SetGateways");
20                 inPar["DefaultIPGateway"] = new string[] { "192.168.16.2", "192.168.16.254" }; // 1.网关;2.备用网关 
21                 outPar = mo.InvokeMethod("SetGateways", inPar, null);
22 
23                 //设置DNS 
24                 inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
25                 inPar["DNSServerSearchOrder"] = new string[] { "211.97.168.129", "202.102.152.3" }; // 1.DNS 2.备用DNS 
26                 outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
27                 break;
28             }
29         }

方法二:

using System;
using System.Collections;
using System.Text;
using System.Management;
using System.Text.RegularExpressions;
namespace Kingthy.Windows.IPChanger.Providers
{
    /// <summary>
    /// IPProvider 的摘要说明。
    /// </summary>
    public class IPProvider
    {
        public IPProvider()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 设置DNS
        /// </summary>
        /// <param name="dns"></param>
        public static void SetDNS(string[] dns)
        {
            SetIPAddress(null, null, null, dns);
        }
        /// <summary>
        /// 设置网关
        /// </summary>
        /// <param name="getway"></param>
        public static void SetGetWay(string getway)
        {
            SetIPAddress(null, null, new string[] { getway }, null);
        }
        /// <summary>
        /// 设置网关
        /// </summary>
        /// <param name="getway"></param>
        public static void SetGetWay(string[] getway)
        {
            SetIPAddress(null, null, getway, null);
        }
        /// <summary>
        /// 设置IP地址和掩码
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="submask"></param>
        public static void SetIPAddress(string ip, string submask)
        {
            SetIPAddress(new string[] { ip }, new string[] { submask }, null, null);
        }
        /// <summary>
        /// 设置IP地址,掩码和网关
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="submask"></param>
        /// <param name="getway"></param>
        public static void SetIPAddress(string ip, string submask, string getway)
        {
            SetIPAddress(new string[] { ip }, new string[] { submask }, new string[] { getway }, null);
        }
        /// <summary>
        /// 设置IP地址,掩码,网关和DNS
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="submask"></param>
        /// <param name="getway"></param>
        /// <param name="dns"></param>
        public static void SetIPAddress(string[] ip, string[] submask, string[] getway, string[] dns)
        {
            ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = wmi.GetInstances();
            ManagementBaseObject inPar = null;
            ManagementBaseObject outPar = null;
            foreach (ManagementObject mo in moc)
            {
                //如果没有启用IP设置的网络设备则跳过
                if (!(bool)mo["IPEnabled"])
                    continue;
                //设置IP地址和掩码
                if (ip != null && submask != null)
                {
                    inPar = mo.GetMethodParameters("EnableStatic");
                    inPar["IPAddress"] = ip;
                    inPar["SubnetMask"] = submask;
                    outPar = mo.InvokeMethod("EnableStatic", inPar, null);
                }
                //设置网关地址
                if (getway != null)
                {
                    inPar = mo.GetMethodParameters("SetGateways");
                    inPar["DefaultIPGateway"] = getway;
                    outPar = mo.InvokeMethod("SetGateways", inPar, null);
                }
                //设置DNS地址
                if (dns != null)
                {
                    inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
                    inPar["DNSServerSearchOrder"] = dns;
                    outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
                }
            }
        }
        /// <summary>
        /// 启用DHCP服务器
        /// </summary>
        public static void EnableDHCP()
        {
            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);
            }
        }
        /// <summary>
        /// 判断是否IP地址格式
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static bool IsIPAddress(string ip)
        {
            string[] arr = ip.Split(.);
            if (arr.Length != 4)
                return false;
            string pattern = @"/d{1,3}";
            for (int i = 0; i < arr.Length; i++)
            {
                string d = arr[i];
                if (i == 0 && d == "0")
                    return false;
                if (!Regex.IsMatch(d, pattern))
                    return false;
                if (d != "0")
                {
                    d = d.TrimStart(0);
                    if (d == "")
                        return false;
                    if (int.Parse(d) > 255)
                        return false;
                }
            }
            return true;
        }
    }
}    

 

修改IP地址

标签:

原文地址:http://www.cnblogs.com/happinesshappy/p/4626701.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!