[原创]C#调用WMI更改本地网络IP设置
因家中与公司的网络配置不同,而我又是带着我的笔记本在家里与公司来来回回.这就导致要经常设置不同的IP和网关数据,一段时间下来后觉得特烦,做为一个程序员.为什么不好好利用自己的知识偷懒一下呢?特别是像我这样一个懒惰的人...
因为网络IP设置是要涉及到硬件,C#是没有现成接口调用的.只能通过调用API或者是WMI这道系统提供给我们的桥梁..在WMI这个大库中用于管理网络配置的则主要是通过"Win32_NetworkAdapterConfiguration"这个管理类.这里面已基本包括了IP,DNS,网关的设置信息...
在C#中使用WMI是很轻松的事情.下面直接给出C#实现代码,呵呵,我的文字表达能力很差,所以我也不多说了,代码量很少,看起来应该很简单的:
1
using System;
2
using System.Collections;
3
using System.Text;
4
using System.Management;
5
using System.Text.RegularExpressions;
6
namespace 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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

注:关系Win32_NetworkAdapterConfiguration这个管理类可以参考这个教程
http://www.microsoft.com/china/technet/community/scriptcenter/topics/networking/01_atnc_intro.mspx
下面附上我根据上面的IPProvider做的一个仿系统更改网络IP界面的小程序界面.呵呵

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义