Method 1: 1. In Solution Explorer, right-click reference and add a reference to system. Management.
2. Add "using system. Management ;".
Static void setnetworkadapter ()
{
Managementbaseobject inpar = NULL;
Managementbaseobject outpar = NULL;
Managementclass MC = new managementclass ("win32_networkadapterconfiguration ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
If (! (Bool) Mo ["ipenabled"])
Continue;
// Set the IP address and subnet mask
Inpar = Mo. getmethodparameters ("enablestatic ");
Inpar ["IPaddress"] = new string [] {"192.168.16.248", "192.168.16.249"}; // 1. Backup 2.ip
Inpar ["subnetmask"] = new string [] {"255.255.255.0", "255.255.255.0 "};
Outpar = Mo. invokemethod ("enablestatic", inpar, null );
// Set the gateway address
Inpar = Mo. getmethodparameters ("setgateways ");
Inpar ["defaultipgateway"] = new string [] {"192.168.16.2", "192.168.16.254"}; // 1. Gateway; 2. Backup Gateway
Outpar = Mo. invokemethod ("setgateways", inpar, null );
// Set DNS
Inpar = Mo. getmethodparameters ("setdnsserversearchorder ");
Inpar ["dnsserversearchorder"] = new string [] {"211.97.168.129", "202.102.152.3"}; // 1.dns 2. Backup DNS
Outpar = Mo. invokemethod ("setdnsserversearchorder", inpar, null );
Break;
}
}
Method 2:
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
{
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;
}
}
}