C # set IP
Last Update:2018-12-05
Source: Internet
Author: User
Using System;
Using System. Collections;
Using System. Text;
Using System. Management;
Using System. Text. RegularExpressions;
Namespace Kingthy. Windows. IPChanger. Providers
{
/// <Summary>
/// Summary of IPProvider.
/// </Summary>
Public class IPProvider
{
Static void Main ()
{
SetIPAddress ("221.179.74.200", "255.255.255.0", "221.179.74.1 ");
}
Public IPProvider ()
{
//
// TODO: add the constructor logic here
//
}
/// <Summary>
/// Set DNS
/// </Summary>
/// <Param name = "dns"> </param>
Public static void SetDNS (string [] dns)
{
SetIPAddress (null, dns );
}
/// <Summary>
/// Configure the Gateway
/// </Summary>
/// <Param name = "getway"> </param>
Public static void SetGetWay (string getway)
{
SetIPAddress (null, null, new string [] {getway}, null );
}
/// <Summary>
/// Configure the Gateway
/// </Summary>
/// <Param name = "getway"> </param>
Public static void SetGetWay (string [] getway)
{
SetIPAddress (null, null, getway, null );
}
/// <Summary>
/// Set the IP address and mask
/// </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>
/// Set the IP address, mask, and gateway
/// </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>
/// Set the IP address, mask, gateway, and 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)
{
// Skip this step if no network device with IP settings Enabled
If (! (Bool) mo ["IPEnabled"])
Continue;
// Set the IP address and mask
If (ip! = Null & submask! = Null)
{
InPar = mo. GetMethodParameters ("EnableStatic ");
InPar ["IPAddress"] = ip;
InPar ["SubnetMask"] = submask;
OutPar = mo. InvokeMethod ("EnableStatic", inPar, null );
}
// Set the gateway address
If (getway! = Null)
{
InPar = mo. GetMethodParameters ("SetGateways ");
InPar ["DefaultIPGateway"] = getway;
OutPar = mo. InvokeMethod ("SetGateways", inPar, null );
}
// Set the DNS address
If (dns! = Null)
{
InPar = mo. GetMethodParameters ("SetDNSServerSearchOrder ");
InPar ["DNSServerSearchOrder"] = dns;
OutPar = mo. InvokeMethod ("SetDNSServerSearchOrder", inPar, null );
}
}
}
/// <Summary>
/// Enable the DHCP server
/// </Summary>
Public static void EnableDHCP ()
{
ManagementClass wmi = new ManagementClass ("Win32_NetworkAdapterConfiguration ");
ManagementObjectCollection moc = wmi. GetInstances ();
Foreach (ManagementObject mo in moc)
{
// Skip this step if no network device with IP settings Enabled
If (! (Bool) mo ["IPEnabled"])
Continue;
// Reset DNS to null
Mo. InvokeMethod ("SetDNSServerSearchOrder", null );
// Enable DHCP
Mo. InvokeMethod ("EnableDHCP", null );
}
}
/// <Summary>
/// Determine whether the IP address format is correct
/// </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;
}
}
}