zoukankan      html  css  js  c++  java
  • Configuring TCP/IP settings by WMI. 用WMI对象访问.设置IP配置

     

    WMI Extends the possibilities of .NET and simplifies the life while working on NetworkAdapters. The Following Code snippet lists all the Network adapters along with the IP Address, Subnet Mask, Default Gateway.

    ////////////////////////////////////////////--List Settings--////////////////////////////////////////////////////
    public void ListIP() 
    {
     ManagementClass objMC = new ManagementClass(
        "Win32_NetworkAdapterConfiguration"); 
     ManagementObjectCollection objMOC = objMC.GetInstances(); 
     
    foreach(ManagementObject objMO in objMOC) 
    { 
              if(!(bool)objMO["ipEnabled"]) 
                     continue; 
    
    
            
              Console.WriteLine(objMO["Caption"] + "," + 
                objMO["ServiceName"] + "," + objMO["MACAddress"]) ; 
              string[] ipaddresses = (string[]) objMO["IPAddress"]; 
              string[] subnets = (string[]) objMO["IPSubnet"]; 
              string[] gateways = (string[]) objMO["DefaultIPGateway"]; 
    
       
              Console.WriteLine("Printing Default Gateway Info:"); 
              Console.WriteLine(objMO["DefaultIPGateway"].ToString()); 
              
              Console.WriteLine("Printing IPGateway Info:"); 
              foreach(string sGate in gateways) 
                   Console.WriteLine (sGate); 
    
     
              Console.WriteLine("Printing Ipaddress Info:"); 
    
              foreach(string sIP in ipaddresses) 
                   Console.WriteLine(sIP); 
     
              Console.WriteLine("Printing SubNet Info:"); 
    
              foreach(string sNet in subnets) 
                   Console.WriteLine(sNet);
    }
    
    ///////////////////////////////////////--Configure Settings--///////////////////////
    public 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); } 
            }
    

    引用注明出处,来自codeproject

  • 相关阅读:
    Windows下Goland的Terminal设置为Git Bash
    BoltDB简单使用教程
    Base64编码转换原理
    [区块链|非对称加密] 对数字证书(CA认证)原理的回顾
    [数据库锁机制] 深入理解乐观锁、悲观锁以及CAS乐观锁的实现机制原理分析
    升级mojave后的小问题解决
    ubuntu安装ssh服务记录
    dubbo+maven多模块项目单元测试
    sass与less
    (转)初识 Lucene
  • 原文地址:https://www.cnblogs.com/asight/p/1785356.html
Copyright © 2011-2022 走看看