• 设置IP 网关 Dns 子网掩码


    Code
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Management;

    namespace Util
    {
        
    class NetWorkUtil
        {
            
    /// <summary>
            
    /// 设置DNS
            
    /// </summary>
            
    /// <param name="dns"></param>
            public static void SetDNS(string[] dns)
            {
                SetIPAddress(
    nullnullnull, dns);
            }
            
    /// <summary>
            
    /// 设置网关
            
    /// </summary>
            
    /// <param name="getway"></param>
            public static void SetGetWay(string getway)
            {
                SetIPAddress(
    nullnullnew string[] { getway }, null);
            }
            
    /// <summary>
            
    /// 设置网关
            
    /// </summary>
            
    /// <param name="getway"></param>
            public static void SetGetWay(string[] getway)
            {
                SetIPAddress(
    nullnull, 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 }, nullnull);
            }
            
    /// <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);
                    }

                    moc.Dispose();
    //释放资源
                    wmi.Dispose();//释放资源
                    break;//只针对第一个网卡设置
                }
            }
            
    /// <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;
            }
        }
    }

  • 相关阅读:
    redis的初认识
    Java中自己定义缓存方式
    Ehcache整合spring
    巴菲特
    EHcache经典配置
    (1)-使用json所要用到的jar包下载
    java和json互转
    TypeToken 是google提供的一个解析Json数据的类库中一个类
    深刻理解Java编程的7个例子
    Java实现几种常见排序方法
  • 原文地址:https://www.cnblogs.com/shineqiujuan/p/1606888.html
Copyright © 2020-2023  润新知