搜索墙首页  编程技术  java  asp  ajax  php  c/c#/c++  数据库  oracle  mysql  db2  操作系统  windows  linux  股吧
首页 > 操作系统 > windows
 1         
1楼  heiyou 2006-01-04

来源:http://www.codeproject.com/csharp/cstcpipwmi.asp
原名:Configuring TCP/IP Settings using WMI and C#
 
 
用C#设置IP的一种方法
//here is the code to configure TCP/IP Settings using WMI
public static void setIP(string IPAddress,string SubnetMask, string Gateway)
{
 
ManagementClass objMC = new ManagementClass(
    "Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();

foreach(ManagementObject objMO in objMOC)
{
      if (!(bool) objMO["IPEnabled"])
           continue;

 
      try
        {
          ManagementBaseObject objNewIP = null;
          ManagementBaseObject objSetIP = null;
          ManagementBaseObject objNewGate = null;
         
          objNewIP = objMO.GetMethodParameters("EnableStatic");
          objNewGate = objMO.GetMethodParameters("SetGateways");
         

          //Set DefaultGateway
          objNewGate["DefaultIPGateway"] = new string[] {Gateway};
          objNewGate["GatewayCostMetric"] = new int[] {1};
         
          //Set IPAddress and Subnet Mask
          objNewIP["IPAddress"] = new string[] {IPAddress};
          objNewIP["SubnetMask"] = new string[] {SubnetMask};
         
          objSetIP = objMO.InvokeMethod("EnableStatic",objNewIP,null);
          objSetIP = objMO.InvokeMethod("SetGateways",objNewGate,null);

         
          Console.WriteLine(
             "Updated IPAddress, SubnetMask and Default Gateway!");

       
        }
        catch(Exception ex)
        {
              MessageBox.Show("Unable to Set IP : " + ex.Message); }
        }
}
 
已经验证代码可用.
验证环境:win2000专业版
编译信息:
Microsoft (R) Visual C# 2005 编译器 版本 8.00.50727.42
用于 Microsoft (R) Windows (R) 2005 Framework 版本 2.0.50727
版权所有 (C) Microsoft Corporation 2001-2005。保留所有权利。
 1         
您的发言将按有关规定都会存档,您须为所发表后果负责,请您遵纪守法并注意语言文明。
标题:转载:Configuring TCP/IP Settings using WMI and C#
热门关注
标题回复点击
类型:伦理片 主演:杨思敏661540
一键还原精灵密码破解0925
Excel中了宏病毒, 怎样清除, 有专杀工具吗?3887
急,在线求助。关于安装程序弹出查找ProPlusWW.msi文件的问题。3688
D:\WINCE500\Updates\Windows CE 5.0_Product_Update_0496
一键还原精灵密码破解0496
Microsoft Radio Interface Layer0464
Adobe Acrobat 8.1.2 Professional 注册机使用方法0447
无差别看QQ空间日志【不管好友非好友,权限非权限】1385
rundll加载模块错误的终极解决方法0291
搜索墙@2009 www.pkwall.com all rights reserved QQ:276471788 [京ICP备09111534号]
声明:本站部分数据来源于网络,仅供参考,如有版权问题,请联系我们,我们将及时删除!转载本站请注明来源