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 { getset; }
        
public string MacAddress { getset; }
        
public string Mask { getset; }
        
public string Gateway { getset; }
        
public string Dns1 { getset; }
        
public string Dns2 { getset; }
    }
}

 

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

posted on 2008-09-02 16:15  ю意思я  阅读(574)  评论(0编辑  收藏  举报