1
static
void
SetNetworkAdapter()
2 {
3
4
5 ManagementBaseObject inPar = null ;
6 ManagementBaseObject outPar = null ;
7 ManagementClass mc = new ManagementClass( " Win32_NetworkAdapterConfiguration " );
8 ManagementObjectCollection moc = mc.GetInstances();
9 foreach ( ManagementObject mo in moc )
10 {
11 if ( ! ( bool ) mo[ " IPEnabled " ] )
12 continue ;
13
14 // 设置ip地址和子网掩码
15 inPar = mo.GetMethodParameters( " EnableStatic " );
16
17
18 inPar[ " IPAddress " ] = new string [] { " 10.22.21.111 " , " 192.168.10.9 " };
19 inPar[ " SubnetMask " ] = new string [] { " 255.255.255.0 " , " 255.255.255.0 " };
20
21
22 outPar = mo.InvokeMethod( " EnableStatic " , inPar, null );
23
24 // 设置网关地址
25 inPar = mo.GetMethodParameters( " SetGateways " );
26 inPar[ " DefaultIPGateway " ] = new string [] { " 10.22.21.1 " , " 192.168.10.1 " };
27 outPar = mo.InvokeMethod( " SetGateways " , inPar, null );
28
29 // 设置DNS
30 inPar = mo.GetMethodParameters( " SetDNSServerSearchOrder " );
31
32
33
34 inPar[ " DNSServerSearchOrder " ] = new string [] { " 179.32.42.4 " , " 179.32.42.5 " };
35
36
37 outPar = mo.InvokeMethod( " SetDNSServerSearchOrder " ,inPar, null );
38 break ;
39 }
40 }
41
42 /**//**//**/ /// <summary>
43
44
45
46 /// 显示当前的ip,子网掩码,网关,DNS,网卡地址等信息
47 /// </summary>
48 static void ShowNetworkAdapterInfo()
49 {
50 copyright
51
52
53 Console.WriteLine( " ****** Current Network Adapter Information ****** " );
54 ManagementClass mc = new ManagementClass( " Win32_NetworkAdapterConfiguration " );
55 ManagementObjectCollection moc = mc.GetInstances();
56 foreach ( ManagementObject mo in moc )
57
58
59 {
60 if ( ! ( bool ) mo[ " IPEnabled " ] )
61 continue ;
62
63 Console.WriteLine( " {0}\n SVC: '{1}' MAC: [{2}] " , ( string ) mo[ " Caption " ],
64 ( string ) mo[ " ServiceName " ], ( string ) mo[ " MACAddress " ] );
65
66 string [] addresses = ( string []) mo[ " IPAddress " ];
67 string [] subnets = ( string []) mo[ " IPSubnet " ];
68 string [] gateways = ( string []) mo[ " DefaultIPGateway " ];
69 string [] dnses = ( string [])mo[ " DNSServerSearchOrder " ];
70
71 Console.WriteLine( " Addresses : " );
72 foreach ( string sad in addresses)
73 Console.WriteLine( " \t'{0}' " , sad );
74
75 Console.WriteLine( " Subnets : " );
76
77
78
79 foreach ( string sub in subnets )
80 Console.WriteLine( " \t'{0}' " , sub );
81 Console.WriteLine( " Gateways: " );
82 foreach ( string gw in gateways )
83
84
85
86 Console.WriteLine( " \t'{0}' " , gw );
87
88 Console.WriteLine( " DNS: " );
89 foreach ( string dns in dnses )
90 Console.WriteLine( " \t'{0}' " , dns );
91 }
92 }
2 {
3
4
5 ManagementBaseObject inPar = null ;
6 ManagementBaseObject outPar = null ;
7 ManagementClass mc = new ManagementClass( " Win32_NetworkAdapterConfiguration " );
8 ManagementObjectCollection moc = mc.GetInstances();
9 foreach ( ManagementObject mo in moc )
10 {
11 if ( ! ( bool ) mo[ " IPEnabled " ] )
12 continue ;
13
14 // 设置ip地址和子网掩码
15 inPar = mo.GetMethodParameters( " EnableStatic " );
16
17
18 inPar[ " IPAddress " ] = new string [] { " 10.22.21.111 " , " 192.168.10.9 " };
19 inPar[ " SubnetMask " ] = new string [] { " 255.255.255.0 " , " 255.255.255.0 " };
20
21
22 outPar = mo.InvokeMethod( " EnableStatic " , inPar, null );
23
24 // 设置网关地址
25 inPar = mo.GetMethodParameters( " SetGateways " );
26 inPar[ " DefaultIPGateway " ] = new string [] { " 10.22.21.1 " , " 192.168.10.1 " };
27 outPar = mo.InvokeMethod( " SetGateways " , inPar, null );
28
29 // 设置DNS
30 inPar = mo.GetMethodParameters( " SetDNSServerSearchOrder " );
31
32
33
34 inPar[ " DNSServerSearchOrder " ] = new string [] { " 179.32.42.4 " , " 179.32.42.5 " };
35
36
37 outPar = mo.InvokeMethod( " SetDNSServerSearchOrder " ,inPar, null );
38 break ;
39 }
40 }
41
42 /**//**//**/ /// <summary>
43
44
45
46 /// 显示当前的ip,子网掩码,网关,DNS,网卡地址等信息
47 /// </summary>
48 static void ShowNetworkAdapterInfo()
49 {
50 copyright
51
52
53 Console.WriteLine( " ****** Current Network Adapter Information ****** " );
54 ManagementClass mc = new ManagementClass( " Win32_NetworkAdapterConfiguration " );
55 ManagementObjectCollection moc = mc.GetInstances();
56 foreach ( ManagementObject mo in moc )
57
58
59 {
60 if ( ! ( bool ) mo[ " IPEnabled " ] )
61 continue ;
62
63 Console.WriteLine( " {0}\n SVC: '{1}' MAC: [{2}] " , ( string ) mo[ " Caption " ],
64 ( string ) mo[ " ServiceName " ], ( string ) mo[ " MACAddress " ] );
65
66 string [] addresses = ( string []) mo[ " IPAddress " ];
67 string [] subnets = ( string []) mo[ " IPSubnet " ];
68 string [] gateways = ( string []) mo[ " DefaultIPGateway " ];
69 string [] dnses = ( string [])mo[ " DNSServerSearchOrder " ];
70
71 Console.WriteLine( " Addresses : " );
72 foreach ( string sad in addresses)
73 Console.WriteLine( " \t'{0}' " , sad );
74
75 Console.WriteLine( " Subnets : " );
76
77
78
79 foreach ( string sub in subnets )
80 Console.WriteLine( " \t'{0}' " , sub );
81 Console.WriteLine( " Gateways: " );
82 foreach ( string gw in gateways )
83
84
85
86 Console.WriteLine( " \t'{0}' " , gw );
87
88 Console.WriteLine( " DNS: " );
89 foreach ( string dns in dnses )
90 Console.WriteLine( " \t'{0}' " , dns );
91 }
92 }