程式設計使用WMI 控制連線屬性 (轉)

amyz發表於2007-08-15
程式設計使用WMI 控制連線屬性 (轉)[@more@]

using System;
using System.Management;

namespace ArLi.CommonPrj {
 public class Change{

 ///


 /// Build of ArLi .6.3
 ///

 public static readonly System.Version myVersion = new System.Version(1,1);

 private ManagementBase iObj = null;
 private ManagementBaseObject oObj = null;
 private ManagementClass mc = new ManagementClass("workAdapterConfiguration");
 private readonly ManagementObjectCollection moc;

 ///


 /// 例:
 ///
 /// 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(); 
 }
 
 /// 控制連線
 /// 列表
 /// 對應子網掩碼列表
 public void ChangeTo(string[] ipAddr,string[] subnetMask) {
 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);
 }
 }

 ///

控制連線
 /// IP地址列表
 /// 對應子網掩碼列表
 /// 閘道器列表
 /// 閘道器介面躍點數列表
 public void ChangeTo(string[] ipAddr, string[] subnetMask, 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);
 }
 }

 ///

控制連線
 /// IP地址列表
 /// 對應子網掩碼列表
 /// 閘道器列表
 /// 閘道器介面躍點數列表
 /// DNS 列表
 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);
 }
 }

 ///

控制連線,使它使用 DHCP
 public void EnableDHCP() {
 foreach(ManagementObject mo in moc) {
 if(! (bool) mo["IPEnabled"]) continue;

 if(! (bool)mo["DHCPEnabled"]) {
 iObj = mo.GetMethodParameters("EnableDHCP");
 oObj = mo.InvokeMethod("EnableDHCP", iObj, null);
 }
 }
 }
 }
}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-959158/,如需轉載,請註明出處,否則將追究法律責任。

相關文章