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

本地IP,掩码,网关,DNS设置

时间:2017-10-29 21:54:35      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:ssi   using   collect   gateway   信息   continue   enabled   子网掩码   enables   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
using System.Net.NetworkInformation;
using System.Text.RegularExpressions;

namespace ZL.Shell业务.NetSetting
{
    public class DHCPSetting
    {
        public string IpAddress { get; set; } //IP地址
        public string SubMask { get; set; } //子网掩码
        public string NetWay { get; set; } //网关
        public string DNSAddress { get; set; } //DNS

        public static DHCPSetting _instance = null;

        private DHCPSetting() { }

        static DHCPSetting()
        {
            if (_instance == null)
            {
                _instance = new DHCPSetting();
            }
        }
        public static DHCPSetting Instance
        {
            get
            {
                return _instance;
            }
        }

        /// <summary>
        /// 获取网络信息
        /// </summary>
        public void GetDHCPAddress()
        {
            ManagementClass manger = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection manColl = manger.GetInstances();
            foreach (ManagementObject item in manColl)
            {
                if (Convert.ToBoolean(item["ipEnabled"]) == true)
                {
                    IpAddress = (item["IPAddress"] as string[])[0];
                    SubMask = (item["IPSubnet"] as string[])[0];
                    NetWay = (item["DefaultIPGateway"] as string[])[0];
                    break;
                }
            }

            NetworkInterface[] network = NetworkInterface.GetAllNetworkInterfaces();
            foreach (var item in network)
            {
                IPAddressCollection dns = item.GetIPProperties().DnsAddresses;
                if (dns.Count > 0)
                {
                    DNSAddress = dns[0].ToString();
                }
            }
        }

        /// <summary>
        /// 设置网络信息
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="submask"></param>
        /// <param name="getway"></param>
        /// <param name="dns"></param>
        public void SetDHCPAddress(string ip, string submask, string getway, string dns)
        {
            ManagementClass manger = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection manColl = manger.GetInstances();
            ManagementBaseObject inPar = null;
            ManagementBaseObject outPar = null;
            foreach (ManagementObject item in manColl)
            {
                //如果没有启用IP设置的网络设备则跳过
                if (!(bool)item["IPEnabled"])
                {
                    continue;
                }
                //设置IP地址和掩码
                if (ip != null && submask != null)
                {
                    inPar = item.GetMethodParameters("EnableStatic");
                    inPar["IPAddress"] = new string[] { ip};
                    inPar["SubnetMask"] = new string[] { submask};
                    outPar = item.InvokeMethod("EnableStatic", inPar, null);
                }
                //设置网关地址
                if (getway != null)
                {
                    inPar = item.GetMethodParameters("SetGateways");
                    inPar["DefaultIPGateway"] = new string[] { getway };
                    outPar = item.InvokeMethod("SetGateways", inPar, null);
                }
                //设置DNS地址
                if (dns != null)
                {
                    inPar = item.GetMethodParameters("SetDNSServerSearchOrder");
                    inPar["DNSServerSearchOrder"] = new string[] { dns };
                    outPar = item.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
                }
                
            }
        }

        /// <summary>
        /// 启用DHCP服务器
        /// </summary>
        public void StartDHCP()
        {
            ManagementClass manger = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection manColl = manger.GetInstances();
            foreach (ManagementObject item in manColl)
            {
                //如果没有启用IP设置的网络设备则跳过
                if (!(bool)item["IPEnabled"])
                    continue;
                //重置DNS为空
                item.InvokeMethod("SetDNSServerSearchOrder", null);
                //开启DHCP
                item.InvokeMethod("EnableDHCP", null);
                break;
            }
        }

        /// <summary>
        /// 判断输入格式是否正确
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public bool IsDHCPAddress(string ip)
        {
            string[] arr = ip.Split(.);
            if (arr.Length != 4)
            {
                return false;
            }
            //正则判断是否由1-3位数组成
            foreach (var item in arr)
            {
                if (!Regex.IsMatch(item, @"\d{1,3}") || item == "" || int.Parse(item) > 255)
                {
                    return false;
                }
            }
            return true;
        }
    }
}

 

本地IP,掩码,网关,DNS设置

标签:ssi   using   collect   gateway   信息   continue   enabled   子网掩码   enables   

原文地址:http://www.cnblogs.com/AlexOneBlogs/p/7751355.html

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