@echo off
rem eth
set eth="本地链接"
rem ip
set ip=XXX.XXX.XXX
rem gw // 网关
set gw=XXX.XXX.XXX.XXX
rem netmasks //子网掩码
set netmasks=255.255.255.0
rem
if %gw%==none netsh interface ip set address %eth% static %ip% %netmasks% %gw% > nul
if not %gw%==none netsh interface ip set address %eth% static %ip% %netmasks% %gw% 1 > nul close 使用C#执行这段 批处理即可
rem eth
set eth="本地链接"
rem ip
set ip=XXX.XXX.XXX
rem gw // 网关
set gw=XXX.XXX.XXX.XXX
rem netmasks //子网掩码
set netmasks=255.255.255.0
rem
if %gw%==none netsh interface ip set address %eth% static %ip% %netmasks% %gw% > nul
if not %gw%==none netsh interface ip set address %eth% static %ip% %netmasks% %gw% 1 > nul close 使用C#执行这段 批处理即可
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);
}
}
}
}
}
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 );
}
}
}
}
还是要感谢你一下