C # set IP

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;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.