111,129
社区成员
发帖
与我相关
我的任务
分享
C#更改IP地址源码
using System;
using System.Management; //引用
namespace ArLi.CommonPrj
{
public class ChangeIP
{
///
/// Build of ArLi 2003.6.3
///
public static readonly System.Version myVersion = new System.Version(1,1);
private ManagementBaseObject iObj = null;
private ManagementBaseObject oObj = null;
private ManagementClass mc = new ManagementClass( "Win32_NetworkAdapterConfiguration ");
private readonly ManagementObjectCollection moc;
///
/// example:
///
/// ArLi.CommonPrj.ChangeIP o = new ArLi.CommonPrj.ChangeIP();
/// string[] ipList = new string[]{ "192.168.0.253 ", "192.168.0.250 "};
/// string[] subnetList = new string[]{ "255.255.255.0 ", "255.255.255.0 "};
/// o.ChangeTo(ipList,subnetList);
///
///
public ChangeIP()
{
moc = mc.GetInstances();
}
/// cortrol
/// IPAddr List
/// subnetMask List
public void ChangeTo(string[] ipAddr,string[] subnetMask)
{
foreach(ManagementObject mo in moc)
{
if(! (bool) mo[ "IPEnabled "]) continue;
iObj = mo.GetMethodParameters( "EnableStatic " );
iObj[ "IPAddress "] = ipAddr;
iObj[ "SubnetMask "] = subnetMask;
oObj = mo.InvokeMethod( "EnableStatic ", iObj, null);
}
}
/// cortrol
/// IPAddr List
/// subnetMask List
/// gateway List
/// gateway CostMetric List, example: 1
public void ChangeTo(string[] ipAddr, string[] subnetMask, string[] gateways, string[] gatewayCostMetric)
{
foreach(ManagementObject mo in moc)
{
if(! (bool) mo[ "IPEnabled "]) continue;
iObj = mo.GetMethodParameters( "EnableStatic ");
iObj[ "IPAddress "] = ipAddr;
iObj[ "SubnetMask "] = subnetMask;
oObj = mo.InvokeMethod( "EnableStatic ", iObj, null);
iObj = mo.GetMethodParameters( "SetGateways ");
iObj[ "DefaultIPGateway "] = gateways;
iObj[ "GatewayCostMetric "] = gatewayCostMetric;
oObj = mo.InvokeMethod( "SetGateways ", iObj, null);
}
}
/// cortrol
/// IPAddr List
/// subnetMask List
/// gateway List
/// gateway CostMetric List, example: 1
/// DNSServer List
public void ChangeTo(string[] ipAddr, string[] subnetMask, string[] gateways, string[] gatewayCostMetric, string[] dnsServer)
{
foreach(ManagementObject mo in moc)
{
if(! (bool) mo[ "IPEnabled "]) continue;
iObj = mo.GetMethodParameters( "EnableStatic ");
iObj[ "IPAddress "] = ipAddr;
iObj[ "SubnetMask "] = subnetMask;
oObj = mo.InvokeMethod( "EnableStatic ", iObj, null);
iObj = mo.GetMethodParameters( "SetGateways ");
iObj[ "DefaultIPGateway "] = gateways;
iObj[ "GatewayCostMetric "] = gatewayCostMetric;
oObj = mo.InvokeMethod( "SetGateways ", iObj, null);
iObj = mo.GetMethodParameters( "SetDNSServerSearchOrder ");
iObj[ "DNSServerSearchOrder "] = dnsServer;
oObj = mo.InvokeMethod( "SetDNSServerSearchOrder ", iObj, null);
}
}
/// DHCPEnabled
public void EnableDHCP()
{
foreach(ManagementObject mo in moc)
{
if(! (bool) mo[ "IPEnabled "]) continue;
if(! (bool)mo[ "DHCPEnabled "])
{
iObj = mo.GetMethodParameters( "EnableDHCP ");
oObj = mo.InvokeMethod( "EnableDHCP ", iObj, null);
}
}
}
}
}
对我有用[0] 丢个板砖[0] 引用 举报 管理 TOP 精华推荐:SQLServer2005 String类的移植尝试
dugupiaoyun
(独孤飘云)
等 级:
#3楼 得分:0回复于:2007-03-31 12:42:09//通过WMI修改IP,而实现不用重新启动?
using System;
using System.Management;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace WmiIpChanger
{
class IpChanger
{
[MTAThread]
static void Main(string[] args)
{
ReportIP();
// SwitchToDHCP();
SwitchToStatic();
Thread.Sleep( 5000 );
ReportIP();
Console.WriteLine( "end. " );
Console.ReadLine();
}
static void SwitchToDHCP()
{
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;
inPar = mo.GetMethodParameters( "EnableDHCP ");
outPar = mo.InvokeMethod( "EnableDHCP ", inPar, null );
break;
}
}
static void SwitchToStatic()
{
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;
inPar = mo.GetMethodParameters( "EnableStatic " );
inPar[ "IPAddress "] = new string[] { "10.1.11.78 " };
inPar[ "SubnetMask "] = new string[] { "255.255.255.0 " };
outPar = mo.InvokeMethod( "EnableStatic ", inPar, null );
break;
}
}
static void ReportIP()
{
Console.WriteLine( "****** Current IP addresses: " );
ManagementClass mc = new ManagementClass( "Win32_NetworkAdapterConfiguration ");
ManagementObjectCollection moc = mc.GetInstances();
foreach( ManagementObject mo in moc )
{
if( ! (bool) mo[ "IPEnabled " ] )
continue;
Console.WriteLine( "{0}\n SVC: '{1}\n ' MAC: [{2}] ", (string) mo[ "Caption "],
(string) mo[ "ServiceName "], (string) mo[ "MACAddress "] );
string[] addresses = (string[]) mo[ "IPAddress " ];
string[] subnets = (string[]) mo[ "IPSubnet " ];
Console.WriteLine( " Addresses : " );
foreach(string sad in addresses)
Console.WriteLine( "\t '{0} ' ", sad );
Console.WriteLine( " Subnets : " );
foreach(string sub in subnets )
Console.WriteLine( "\t '{0} ' ", sub );
}
}
}