C # source code for IP address Modification

Source: Internet
Author: User

UsingSystem;
UsingSystem. Management;

NamespaceArli. commonprj {
Public ClassChangeip {

Public StaticReadonly system. Version myversion= NewSystem. Version (1, 1 );

PrivateManagementbaseobject iobj= Null;
PrivateManagementbaseobject oobj= Null;
PrivateManagementclass MC= NewManagementclass ("win32_networkadapterconfiguration ");
PrivateReadonly managementobjectcollection MOC;

///


// example:
///
// arli. commonprj. changeip o = New arli. commonprj. changeip ();
// string [] iplist = New String [] {"192.168.0.253 ", "192.168.0.250" };
// string [] subnetlist = New String [] {"255.255.255.0 ", "255.255.255.0" };
// o. changeto (iplist, subnetlist );
///

///
Public changeip () {
MOC = MC. getinstances ();
}

/// <Summary> cortrol </Summary>
/// <Param name = "ipaddr"> ipaddr list </param>
/// <Param name = "subnetmask"> subnetmask list </param>
Public VoidChangeto (String[] Ipaddr,String[] Subnetmask ){
Foreach(Managementobject MoInMOC ){
If(! (Bool) Mo ["ipenabled"]) continue;

Iobj=Mo. getmethodparameters ("enablestatic ");
Iobj ["IPaddress"]=Ipaddr;
Iobj ["subnetmask"]=Subnetmask;
Oobj=Mo. invokemethod ("enablestatic", iobj,Null);
}
}

///

cortrol
/// ipaddr list
/// subnetmask list
/// gateway list
/// gateway costmetric list, example: 1
Public void changeto ( string [] ipaddr, string [] subnetmas K, string [] gateways, string [] gatewaycostmetric) {
foreach (managementobject Mo in MOC) {
If (! ( bool ) Mo ["ipenabled"]) continue;

Iobj=Mo. getmethodparameters ("enablestatic ");
Iobj ["IPaddress"]=Ipaddr;
Iobj ["subnetmask"]=Subnetmask;
Oobj=Mo. invokemethod ("enablestatic", iobj,Null);

Iobj=Mo. getmethodparameters ("setgateways ");
Iobj ["defaultipgateway"]=Gateways;
Iobj ["gatewaycostmetric"]=Gatewaycostmetric;
Oobj=Mo. invokemethod ("setgateways", iobj,Null);
}
}

/// <Summary> cortrol </Summary>
/// <Param name = "ipaddr"> ipaddr list </param>
/// <Param name = "subnetmask"> subnetmask list </param>
/// <Param name = "gateways"> gateway list </param>
/// <Param name = "gatewaycostmetric"> gateway costmetric list, example: 1 </param>
/// <Param name = "dnsserver"> dnsserver list </param>
Public Void Changeto ( String [] Ipaddr, String [] Subnetmask, String [] Gateways, String [] Gatewaycostmetric, String [] Dnsserver ){
Foreach (Managementobject Mo In MOC ){
If (! ( Bool ) Mo ["ipenabled"]) continue;

Iobj=Mo. getmethodparameters ("enablestatic ");
Iobj ["IPaddress"]=Ipaddr;
Iobj ["subnetmask"]=Subnetmask;
Oobj=Mo. invokemethod ("enablestatic", iobj,Null);

Iobj=Mo. getmethodparameters ("setgateways ");
Iobj ["defaultipgateway"]=Gateways;
Iobj ["gatewaycostmetric"]=Gatewaycostmetric;
Oobj=Mo. invokemethod ("setgateways", iobj,Null);

Iobj=Mo. getmethodparameters ("setdnsserversearchorder ");
Iobj ["dnsserversearchorder"]=Dnsserver;
Oobj=Mo. invokemethod ("setdnsserversearchorder", iobj,Null);
}
}

/// <Summary> dhcpenabled </Summary>
Public VoidEnabledhcp (){
Foreach(Managementobject MoInMOC ){
If(! (Bool) Mo ["ipenabled"]) continue;

If(! (Bool) Mo ["dhcpenabled"]) {
Iobj=Mo. getmethodparameters ("enabledhcp ");
Oobj=Mo. invokemethod ("enabledhcp", iobj,Null);
}
}
}
}
}

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.