C#修改本机的IP,网关等网络地址的代码片段

 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   } 

 

转载于:https://www.cnblogs.com/godwar/archive/2010/05/25/1743699.html

、在 “解决方案资源管理器” 右击 “引用” 添加 “System.Management” 的引用。 2、添加 “using System.Management;”。 static void SetNetworkAdapter() { ManagementBaseObject inPar = null; ManagementBaseObject outPar = null; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if (!(bool)mo["IPEnabled"]) continue; //设置ip地址和子网掩码 inPar = mo.GetMethodParameters("EnableStatic"); inPar["IPAddress"] = new string[] { "192.168.16.248", "192.168.16.249" };// 1.备用 2.IP inPar["SubnetMask"] = new string[] { "255.255.255.0", "255.255.255.0" }; outPar = mo.InvokeMethod("EnableStatic", inPar, null); //设置网关地址 inPar = mo.GetMethodParameters("SetGateways"); inPar["DefaultIPGateway"] = new string[] { "192.168.16.2", "192.168.16.254" }; // 1.网关;2.备用网关 outPar = mo.InvokeMethod("SetGateways", inPar, null); //设置DNS inPar = mo.GetMethodParameters("SetDNSServerSearchOrder"); inPar["DNSServerSearchOrder"] = new string[] { "211.97.168.129", "202.102.152.3" }; // 1.DNS 2.备用DNS outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null); break; } } C# 开机自动运行程序2007-12-31 12:32string StartupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup); System.IO.File.Copy("应用程序路径(包括程序名)", StartupPath + "执行程序文件名称", true); . . . . . . . . .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值