获取和设置IP地址

 

  Program.cs

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Management;

namespace  CommandLine {
    
class  Program {
        
static   void  Main( string [] args) {
            ShowNetwork();
            SetNetwork(
new  Networker {
                IpAddress 
=   " 192.168.15.27 " ,
                Mask 
=   " 255.255.255.0 " ,
                Gateway 
=   " 192.168.15.1 " ,
                Dns1 
=   " 202.96.209.133 " ,
                Dns2 
=   " 203.94.0.26 "
            });

            Console.WriteLine(
" == End == " );
            Console.ReadKey();
        }

        
public   static   void  ShowNetwork() {
            ManagementObjectSearcher query 
=   new  ManagementObjectSearcher( " SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE' " );
            ManagementObjectCollection queryCollection 
=  query.Get();
            Console.WriteLine(
" ========== " );
            
foreach  (ManagementObject mo  in  queryCollection) {
                
string  description  =  mo[ " Description " ].ToString();
                
string  mac  =  mo[ " MACAddress " ].ToString();
                
string [] ips  =  ( string [])mo[ " IPAddress " ];
                
string [] masks  =  ( string [])mo[ " IPSubnet " ];
                
string [] gateways  =  ( string [])mo[ " DefaultIPGateway " ];
                
string [] dnses  =  ( string [])mo[ " DNSServerSearchOrder " ];
                Console.WriteLine(
" Description:{0} " , description);
                Console.WriteLine(
" MACAddress:{0} " , mac);
                
foreach  ( string  each  in  ips) {
                    Console.WriteLine(
" IPAddress:{0} " , each);
                }
                
foreach  ( string  each  in  masks) {
                    Console.WriteLine(
" Mask:{0} " , each);
                }
                
if  (gateways  !=   null ) {
                    
foreach  ( string  each  in  gateways) {
                        Console.WriteLine(
" Gateway:{0} " , each);
                    }
                }
                
if  (dnses  !=   null ) {
                    
foreach  ( string  each  in  dnses) {
                        Console.WriteLine(
" DNSServer:{0} " , each);
                    }
                }
                Console.WriteLine(
" ========== " );
            }
        }

        
public   static   void  SetNetwork(Networker worker) {
            
string  result  =   " failed " ;

            ManagementObjectSearcher query 
=   new  ManagementObjectSearcher( " SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE' " );
            ManagementObjectCollection queryCollection 
=  query.Get();
            
string [] ipAddress  =   new   string [] { worker.IpAddress };
            
string [] mask  =   new   string [] { worker.Mask };
            
string [] gateway  =   null ;
            
if  ( ! string .IsNullOrEmpty(worker.Gateway)) {
                gateway 
=   new   string [] { worker.Gateway };
            }
            
string [] dnses  =   null ;
            List
< string >  list  =   new  List < string > ();
            
if  ( ! string .IsNullOrEmpty(worker.Dns1)) {
                list.Add(worker.Dns1);
            }
            
if  ( ! string .IsNullOrEmpty(worker.Dns2)) {
                list.Add(worker.Dns2);
            }
            
if  (list.Count  !=   0 ) {
                dnses 
=  list.ToArray();
            }

            ManagementBaseObject inPar 
=   null ;
            ManagementBaseObject outPar 
=   null ;
            
foreach  (ManagementObject mo  in  queryCollection) {
                
try  {
                    
// 设置IP地址和掩码
                     if  (ipAddress  !=   null   &&  mask  !=   null ) {
                        inPar 
=  mo.GetMethodParameters( " EnableStatic " );
                        inPar[
" IPAddress " =  ipAddress;
                        inPar[
" SubnetMask " =  mask;
                        outPar 
=  mo.InvokeMethod( " EnableStatic " , inPar,  null );
                    }

                    
// 设置网关地址
                     if  (gateway  !=   null ) {
                        inPar 
=  mo.GetMethodParameters( " SetGateways " );
                        inPar[
" DefaultIPGateway " =  gateway;
                        outPar 
=  mo.InvokeMethod( " SetGateways " , inPar,  null );
                    }

                    
// 设置DNS地址
                     if  (dnses  !=   null ) {
                        inPar 
=  mo.GetMethodParameters( " SetDNSServerSearchOrder " );
                        inPar[
" DNSServerSearchOrder " =  dnses;
                        outPar 
=  mo.InvokeMethod( " SetDNSServerSearchOrder " , inPar,  null );
                    }
                    result 
=   " succeed " ;
                } 
catch  {
                    result 
=   " failed " ;
                }
                
break ;
            }
            Console.WriteLine(
" SetNetwork {0} " , result);
        }
    }
}
  Networker.cs
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;

namespace  CommandLine {
    
public   class  Networker {
        
public   string  IpAddress {  get set ; }
        
public   string  MacAddress {  get set ; }
        
public   string  Mask {  get set ; }
        
public   string  Gateway {  get set ; }
        
public   string  Dns1 {  get set ; }
        
public   string  Dns2 {  get set ; }
    }
}

 

【意思原创,转载请注明出处,谢谢】

转载于:https://www.cnblogs.com/acexist/archive/2008/09/02/1282117.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值