如何通过编程实现动态更改ip、网关及DHCP配置

lightgun 2003-09-08 10:43:55
如何通过编程实现动态更改ip、网关及DHCP配置(操作系统为2000和98)
针对DHCP选项,如何通过编程使原先的DHCP选项中的Enable变为Disable
ip及网关的配置,如何通过编程删除原先的ip及网关。
由于在MSDN中未找到合适的函数,希望大家指点
...全文
171 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xz_king 2003-09-22
  • 打赏
  • 举报
回复
学习
peering08cn 2003-09-20
  • 打赏
  • 举报
回复
关注
redfoilsman 2003-09-19
  • 打赏
  • 举报
回复
上面的程序怎样引用 using System.Management;

谢谢
huxin1012 2003-09-19
  • 打赏
  • 举报
回复
楼上的方法,可以修改,但我修改的结果是
修改之后,无法看到网上邻居的内容了。必须先禁用本地连接,再启动本地连接后才有用。
谁能解释一下?
再有,如何使网关的内容为空呢?
carper 2003-09-09
  • 打赏
  • 举报
回复
C#实现更改IP功能(原码)
作者:痕迹

http://www.uncj.net/news/show.aspx?id=132

using System;
using System.Management;

namespace ArLi.CommonPrj {
public class ChangeIP {

/// <summary>
/// Build of ArLi 2003.6.3
/// </summary>
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;

/// <summary>
/// example:
/// <code>
/// 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);
/// </code>
/// </summary>
public ChangeIP(){
moc = mc.GetInstances();
}

/// <summary>cortrol</summary>
/// <param name="ipAddr">IPAddr List</param>
/// <param name="subnetMask">subnetMask List</param>
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);
}
}

/// <summary>cortrol</summary>
/// <param name="ipAddr">IPAddr List</param>
/// <param name="subnetMask">subnetMask List</param>
/// <param name="gateways">gateway List</param>
/// <param name="gatewayCostMetric">gateway CostMetric List, example: 1</param>
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);
}
}

/// <summary>cortrol</summary>
/// <param name="ipAddr">IPAddr List</param>
/// <param name="subnetMask">subnetMask List</param>
/// <param name="gateways">gateway List</param>
/// <param name="gatewayCostMetric">gateway CostMetric List, example: 1</param>
/// <param name="dnsServer">DNSServer List</param>
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);
}
}

/// <summary>DHCPEnabled</summary>
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);
}
}
}
}
}

12,162

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧