. NET 技术讨论

学于明志,交流增加见识,讨论改变思维
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

.net修改网络配置,如IP,DNS等

Posted on 2007-03-13 19:03    阅读(1400)  评论(0编辑  收藏  举报
提供一个类库参考,具体如下:
  1 using System;
  2 using System.Collections;
  3 using System.Management;
  4 
  5 namespace SwitchNetConfig
  6 {
  7     /// <summary>
  8     /// A Helper class which provides convenient methods to set/get network
  9     /// configuration
 10     /// </summary>
 11     public class WMIHelper
 12     {
 13         #region Public Static
 14 
 15         /// <summary>
 16         /// Enable DHCP on the NIC
 17         /// </summary>
 18         /// <param name="nicName">Name of the NIC</param>
 19         public static void SetDHCP( string nicName )
 20         {
 21             ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
 22             ManagementObjectCollection moc = mc.GetInstances();
 23 
 24             foreach(ManagementObject mo in moc)
 25             {
 26                 // Make sure this is a IP enabled device. Not something like memory card or VM Ware
 27                 if( (bool)mo["IPEnabled"] )
 28                 {
 29                     if( mo["Caption"].Equals( nicName ) )
 30                     {
 31                         ManagementBaseObject newDNS = mo.GetMethodParameters( "SetDNSServerSearchOrder" );
 32                         newDNS[ "DNSServerSearchOrder" ] = null;
 33                         ManagementBaseObject enableDHCP = mo.InvokeMethod( "EnableDHCP"nullnull);
 34                         ManagementBaseObject setDNS = mo.InvokeMethod( "SetDNSServerSearchOrder", newDNS, null);
 35                     }
 36                 }
 37             }
 38         }
 39         
 40         /// <summary>
 41         /// Set IP for the specified network card name
 42         /// </summary>
 43         /// <param name="nicName">Caption of the network card</param>
 44         /// <param name="IpAddresses">Comma delimited string containing one or more IP</param>
 45         /// <param name="SubnetMask">Subnet mask</param>
 46         /// <param name="Gateway">Gateway IP</param>
 47         /// <param name="DnsSearchOrder">Comma delimited DNS IP</param>
 48         public static void SetIP( string nicName, string IpAddresses, string SubnetMask, string Gateway, string DnsSearchOrder)
 49         {
 50             ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
 51             ManagementObjectCollection moc = mc.GetInstances();
 52 
 53             foreach(ManagementObject mo in moc)
 54             {
 55                 // Make sure this is a IP enabled device. Not something like memory card or VM Ware
 56                 if( (bool)mo["IPEnabled"] )
 57                 {
 58                     if( mo["Caption"].Equals( nicName ) )
 59                     {
 60 
 61                         ManagementBaseObject newIP = mo.GetMethodParameters( "EnableStatic" );
 62                         ManagementBaseObject newGate = mo.GetMethodParameters( "SetGateways" );
 63                         ManagementBaseObject newDNS = mo.GetMethodParameters( "SetDNSServerSearchOrder" );
 64                                 
 65                         newGate[ "DefaultIPGateway" ] = new string[] { Gateway };
 66                         newGate[ "GatewayCostMetric" ] = new int[] { 1 };
 67 
 68                         newIP[ "IPAddress" ] = IpAddresses.Split( ',' );
 69                         newIP[ "SubnetMask" ] = new string[] { SubnetMask };
 70 
 71                         newDNS[ "DNSServerSearchOrder" ] = DnsSearchOrder.Split(',');
 72 
 73                         ManagementBaseObject setIP = mo.InvokeMethod( "EnableStatic", newIP, null);
 74                         ManagementBaseObject setGateways = mo.InvokeMethod( "SetGateways", newGate, null);
 75                         ManagementBaseObject setDNS = mo.InvokeMethod( "SetDNSServerSearchOrder", newDNS, null);
 76 
 77                         break;
 78                     }
 79                 }
 80             }
 81         }
 82 
 83         /// <summary>
 84         /// Returns the network card configuration of the specified NIC
 85         /// </summary>
 86         /// <param name="nicName">Name of the NIC</param>
 87         /// <param name="ipAdresses">Array of IP</param>
 88         /// <param name="subnets">Array of subnet masks</param>
 89         /// <param name="gateways">Array of gateways</param>
 90         /// <param name="dnses">Array of DNS IP</param>
 91         public static void GetIP( string nicName, out string [] ipAdresses, out string [] subnets, out string [] gateways, out string [] dnses )
 92         {
 93             ipAdresses = null;
 94             subnets = null;
 95             gateways = null;
 96             dnses = null;
 97 
 98             ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
 99             ManagementObjectCollection moc = mc.GetInstances();
100 
101             foreach(ManagementObject mo in moc)
102             {
103                 // Make sure this is a IP enabled device. Not something like memory card or VM Ware
104                 if( (bool)mo["ipEnabled"] )
105                 {
106                     if( mo["Caption"].Equals( nicName ) )
107                     {
108                         ipAdresses = (string[]) mo["IPAddress"];
109                         subnets = (string[]) mo["IPSubnet"];
110                         gateways = (string[]) mo["DefaultIPGateway"];
111                         dnses = (string[]) mo["DNSServerSearchOrder"];
112 
113                         break;
114                     }
115                 }
116             }
117         }
118 
119         /// <summary>
120         /// Returns the list of Network Interfaces installed
121         /// </summary>
122         /// <returns>Array list of string</returns>
123         public static ArrayList GetNICNames()
124         {
125             ArrayList nicNames = new ArrayList();
126 
127             ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
128             ManagementObjectCollection moc = mc.GetInstances();
129 
130             foreach(ManagementObject mo in moc)
131             {
132                 if((bool)mo["ipEnabled"])
133                 {
134                     nicNames.Add( mo["Caption"] );
135                 }
136             }
137 
138             return nicNames;
139         }
140 
141         #endregion
142     }
143 }
144 
145