[原创]C#调用WMI更改本地网络IP设置
因家中与公司的网络配置不同,而我又是带着我的笔记本在家里与公司来来回回.这就导致要经常设置不同的IP和网关数据,一段时间下来后觉得特烦,做为一个程序员.为什么不好好利用自己的知识偷懒一下呢?特别是像我这样一个懒惰的人...
因为网络IP设置是要涉及到硬件,C#是没有现成接口调用的.只能通过调用API或者是WMI这道系统提供给我们的桥梁..在WMI这个大库中用于管理网络配置的则主要是通过"Win32_NetworkAdapterConfiguration"这个管理类.这里面已基本包括了IP,DNS,网关的设置信息...
在C#中使用WMI是很轻松的事情.下面直接给出C#实现代码,呵呵,我的文字表达能力很差,所以我也不多说了,代码量很少,看起来应该很简单的:
1using System;
2using System.Collections;
3using System.Text;
4using System.Management;
5using System.Text.RegularExpressions;
6namespace Kingthy.Windows.IPChanger.Providers
7{
8 /// <summary>
9 /// IPProvider 的摘要说明。
10 /// </summary>
11 public class IPProvider
12 {
13 public IPProvider()
14 {
15 //
16 // TODO: 在此处添加构造函数逻辑
17 //
18 }
19 /// <summary>
20 /// 设置DNS
21 /// </summary>
22 /// <param name="dns"></param>
23 public static void SetDNS(string[] dns)
24 {
25 SetIPAddress(null,null,null,dns);
26 }
27 /// <summary>
28 /// 设置网关
29 /// </summary>
30 /// <param name="getway"></param>
31 public static void SetGetWay(string getway)
32 {
33 SetIPAddress(null,null,new string[]{getway},null);
34 }
35 /// <summary>
36 /// 设置网关
37 /// </summary>
38 /// <param name="getway"></param>
39 public static void SetGetWay(string[] getway)
40 {
41 SetIPAddress(null,null,getway,null);
42 }
43 /// <summary>
44 /// 设置IP地址和掩码
45 /// </summary>
46 /// <param name="ip"></param>
47 /// <param name="submask"></param>
48 public static void SetIPAddress(string ip,string submask)
49 {
50 SetIPAddress(new string[]{ip},new string[]{submask},null,null);
51 }
52 /// <summary>
53 /// 设置IP地址,掩码和网关
54 /// </summary>
55 /// <param name="ip"></param>
56 /// <param name="submask"></param>
57 /// <param name="getway"></param>
58 public static void SetIPAddress(string ip,string submask,string getway)
59 {
60 SetIPAddress(new string[]{ip},new string[]{submask},new string[]{getway},null);
61 }
62 /// <summary>
63 /// 设置IP地址,掩码,网关和DNS
64 /// </summary>
65 /// <param name="ip"></param>
66 /// <param name="submask"></param>
67 /// <param name="getway"></param>
68 /// <param name="dns"></param>
69 public static void SetIPAddress(string[] ip,string[] submask,string[] getway,string[] dns)
70 {
71 ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
72 ManagementObjectCollection moc = wmi.GetInstances();
73 ManagementBaseObject inPar = null;
74 ManagementBaseObject outPar = null;
75 foreach(ManagementObject mo in moc)
76 {
77 //如果没有启用IP设置的网络设备则跳过
78 if(!(bool)mo["IPEnabled"])continue;
79
80 //设置IP地址和掩码
81 if(ip != null && submask != null)
82 {
83 inPar = mo.GetMethodParameters("EnableStatic");
84 inPar["IPAddress"] = ip;
85 inPar["SubnetMask"] = submask;
86 outPar = mo.InvokeMethod("EnableStatic",inPar,null);
87 }
88
89 //设置网关地址
90 if(getway != null)
91 {
92 inPar = mo.GetMethodParameters("SetGateways");
93 inPar["DefaultIPGateway"] = getway;
94 outPar = mo.InvokeMethod("SetGateways",inPar,null);
95 }
96
97 //设置DNS地址
98 if(dns != null)
99 {
100 inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
101 inPar["DNSServerSearchOrder"] = dns;
102 outPar = mo.InvokeMethod("SetDNSServerSearchOrder",inPar,null);
103 }
104 }
105 }
106
107 /// <summary>
108 /// 启用DHCP服务器
109 /// </summary>
110 public static void EnableDHCP()
111 {
112 ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
113 ManagementObjectCollection moc = wmi.GetInstances();
114 foreach(ManagementObject mo in moc)
115 {
116 //如果没有启用IP设置的网络设备则跳过
117 if(!(bool)mo["IPEnabled"])continue;
118
119 //重置DNS为空
120 mo.InvokeMethod("SetDNSServerSearchOrder",null);
121 //开启DHCP
122 mo.InvokeMethod("EnableDHCP",null);
123 }
124 }
125
126 /// <summary>
127 /// 判断是否IP地址格式
128 /// </summary>
129 /// <param name="ip"></param>
130 /// <returns></returns>
131 public static bool IsIPAddress(string ip)
132 {
133 string[] arr = ip.Split('.');
134 if(arr.Length != 4)return false;
135
136 string pattern = @"\d{1,3}";
137 for(int i=0; i<arr.Length; i++)
138 {
139 string d = arr[i];
140 if(i == 0 && d == "0")return false;
141 if(!Regex.IsMatch(d,pattern))return false;
142
143 if(d != "0")
144 {
145 d = d.TrimStart('0');
146 if(d == "")return false;
147
148 if(int.Parse(d) > 255)return false;
149 }
150 }
151
152 return true;
153 }
154 }
155}
156
2using System.Collections;
3using System.Text;
4using System.Management;
5using System.Text.RegularExpressions;
6namespace Kingthy.Windows.IPChanger.Providers
7{
8 /// <summary>
9 /// IPProvider 的摘要说明。
10 /// </summary>
11 public class IPProvider
12 {
13 public IPProvider()
14 {
15 //
16 // TODO: 在此处添加构造函数逻辑
17 //
18 }
19 /// <summary>
20 /// 设置DNS
21 /// </summary>
22 /// <param name="dns"></param>
23 public static void SetDNS(string[] dns)
24 {
25 SetIPAddress(null,null,null,dns);
26 }
27 /// <summary>
28 /// 设置网关
29 /// </summary>
30 /// <param name="getway"></param>
31 public static void SetGetWay(string getway)
32 {
33 SetIPAddress(null,null,new string[]{getway},null);
34 }
35 /// <summary>
36 /// 设置网关
37 /// </summary>
38 /// <param name="getway"></param>
39 public static void SetGetWay(string[] getway)
40 {
41 SetIPAddress(null,null,getway,null);
42 }
43 /// <summary>
44 /// 设置IP地址和掩码
45 /// </summary>
46 /// <param name="ip"></param>
47 /// <param name="submask"></param>
48 public static void SetIPAddress(string ip,string submask)
49 {
50 SetIPAddress(new string[]{ip},new string[]{submask},null,null);
51 }
52 /// <summary>
53 /// 设置IP地址,掩码和网关
54 /// </summary>
55 /// <param name="ip"></param>
56 /// <param name="submask"></param>
57 /// <param name="getway"></param>
58 public static void SetIPAddress(string ip,string submask,string getway)
59 {
60 SetIPAddress(new string[]{ip},new string[]{submask},new string[]{getway},null);
61 }
62 /// <summary>
63 /// 设置IP地址,掩码,网关和DNS
64 /// </summary>
65 /// <param name="ip"></param>
66 /// <param name="submask"></param>
67 /// <param name="getway"></param>
68 /// <param name="dns"></param>
69 public static void SetIPAddress(string[] ip,string[] submask,string[] getway,string[] dns)
70 {
71 ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
72 ManagementObjectCollection moc = wmi.GetInstances();
73 ManagementBaseObject inPar = null;
74 ManagementBaseObject outPar = null;
75 foreach(ManagementObject mo in moc)
76 {
77 //如果没有启用IP设置的网络设备则跳过
78 if(!(bool)mo["IPEnabled"])continue;
79
80 //设置IP地址和掩码
81 if(ip != null && submask != null)
82 {
83 inPar = mo.GetMethodParameters("EnableStatic");
84 inPar["IPAddress"] = ip;
85 inPar["SubnetMask"] = submask;
86 outPar = mo.InvokeMethod("EnableStatic",inPar,null);
87 }
88
89 //设置网关地址
90 if(getway != null)
91 {
92 inPar = mo.GetMethodParameters("SetGateways");
93 inPar["DefaultIPGateway"] = getway;
94 outPar = mo.InvokeMethod("SetGateways",inPar,null);
95 }
96
97 //设置DNS地址
98 if(dns != null)
99 {
100 inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
101 inPar["DNSServerSearchOrder"] = dns;
102 outPar = mo.InvokeMethod("SetDNSServerSearchOrder",inPar,null);
103 }
104 }
105 }
106
107 /// <summary>
108 /// 启用DHCP服务器
109 /// </summary>
110 public static void EnableDHCP()
111 {
112 ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
113 ManagementObjectCollection moc = wmi.GetInstances();
114 foreach(ManagementObject mo in moc)
115 {
116 //如果没有启用IP设置的网络设备则跳过
117 if(!(bool)mo["IPEnabled"])continue;
118
119 //重置DNS为空
120 mo.InvokeMethod("SetDNSServerSearchOrder",null);
121 //开启DHCP
122 mo.InvokeMethod("EnableDHCP",null);
123 }
124 }
125
126 /// <summary>
127 /// 判断是否IP地址格式
128 /// </summary>
129 /// <param name="ip"></param>
130 /// <returns></returns>
131 public static bool IsIPAddress(string ip)
132 {
133 string[] arr = ip.Split('.');
134 if(arr.Length != 4)return false;
135
136 string pattern = @"\d{1,3}";
137 for(int i=0; i<arr.Length; i++)
138 {
139 string d = arr[i];
140 if(i == 0 && d == "0")return false;
141 if(!Regex.IsMatch(d,pattern))return false;
142
143 if(d != "0")
144 {
145 d = d.TrimStart('0');
146 if(d == "")return false;
147
148 if(int.Parse(d) > 255)return false;
149 }
150 }
151
152 return true;
153 }
154 }
155}
156
注:关系Win32_NetworkAdapterConfiguration这个管理类可以参考这个教程
http://www.microsoft.com/china/technet/community/scriptcenter/topics/networking/01_atnc_intro.mspx
下面附上我根据上面的IPProvider做的一个仿系统更改网络IP界面的小程序界面.呵呵