Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1012905
  • 博文数量: 177
  • 博客积分: 3629
  • 博客等级: 中校
  • 技术积分: 1839
  • 用 户 组: 普通用户
  • 注册时间: 2005-02-23 21:21
文章分类

全部博文(177)

文章存档

2021年(1)

2020年(5)

2019年(4)

2018年(7)

2017年(1)

2016年(4)

2014年(1)

2013年(8)

2012年(10)

2011年(50)

2009年(12)

2008年(10)

2006年(56)

2005年(8)

分类: WINDOWS

2006-01-04 22:27:54

来源:
原名: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。保留所有权利。
阅读(1466) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~