ITPub博客

首页 > IT职业 > IT职场 > 编程使用WMI 控制连接属性 (转)

编程使用WMI 控制连接属性 (转)

原创 IT职场 作者:amyz 时间:2007-08-15 18:03:50 0 删除 编辑
编程使用WMI 控制连接属性 (转)[@more@]

using System;
using System.Management;

namespace ArLi.CommonPrj {
 public class Changeip {

 ///


 /// Build of ArLi 2003.6.3
 ///

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

 private ManagementBaseobject iObj = null;
 private ManagementBaseObject oObj = null;
 private ManagementClass mc = new ManagementClass("win32.NETworkAdapterConfiguration");
 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(); 
 }
 
 /// 控制连接
 /// IP地址列表
 /// 对应子网掩码列表
 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地址列表
 /// 对应子网掩码列表
 /// 网关列表
 /// 网关接口跃点数列表
 /// dnsServer">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博客 ” ,链接:https://blog.itpub.net/10752019/viewspace-959158/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    3982
  • 访问量
    7896346