惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 君

50口诀让你轻松记四六级单词 苦瓜炒蛋 随笔 将货币数字描述转化成中文描述 - 君 - 博客园 年旦前 经历一劫 2007-12-16 随缘也 留脚 一篇不错的文章 .net写发送邮件工具 2007-2-3 2006-12-23 .NET中获取系统硬件信息 网站宣传 用C#播放声音文件 DotNetMagic做应用程序界面(类似office2003风格) Grove——.NET中的ORM实现(转载,学习...........)
.net修改网络配置,如IP,DNS等
· 2007-03-13 · via 博客园 - 君

提供一个类库参考,具体如下:

  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