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

推荐订阅源

aimingoo的专栏
aimingoo的专栏
S
Securelist
博客园 - Franky
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
IT之家
IT之家
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
The Cloudflare Blog
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
AI
AI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Schneier on Security
Schneier on Security
Attack and Defense Labs
Attack and Defense Labs
Vercel News
Vercel News
腾讯CDC
Google DeepMind News
Google DeepMind News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
量子位
S
Schneier on Security
Hacker News: Ask HN
Hacker News: Ask HN
Cyberwarzone
Cyberwarzone
S
Security Affairs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
News and Events Feed by Topic
T
Tenable Blog
PCI Perspectives
PCI Perspectives
MyScale Blog
MyScale Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
W
WeLiveSecurity
N
News | PayPal Newsroom
P
Proofpoint News Feed
O
OpenAI News
C
CERT Recently Published Vulnerability Notes
B
Blog
Cisco Talos Blog
Cisco Talos Blog
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Spread Privacy
Spread Privacy

博客园 - 君

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