C#修改IP地址问题

flyingsheep_mrl 2015-04-08 12:14:17
在网上找到的代码,基本都是一样,但我试过总是没有反应,求指导:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;

private void button1_Click(object sender, EventArgs e)
{
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.0.2" };
inPar["SubnetMask"] = new string[] { "255.255.255.0" };
outPar = mo.InvokeMethod("EnableStatic", inPar, null);
//设置网关
inPar = mo.GetMethodParameters("SetGateways");
inPar["DefaultIPGateway"] = new string[] { "192.168.0.1" };
outPar = mo.InvokeMethod("SetGateways", inPar, null);
//设置DNS
inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
inPar["DNSServerSearchOrder"] = new string []{"192.168.0.1"
outPar = mo.InvokeMethod("SetDNSServerSearchOrder",inPar,null);
break;
}
};

只有这么点可用分了,求别嫌弃,感激不尽!
...全文
340 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
flyingsheep_mrl 2015-04-10
  • 打赏
  • 举报
回复
引用 2 楼 findcaiyzh 的回复:
看看这个讨论有帮助没 http://stackoverflow.com/questions/11155520/win32-networkadapterconfiguration-net-4-setting-static-ip-works-on-xp-but-not
问题原来在这里啊!“works on XP but not on Windows 7”!但在WIN7下有什么解决方案啊?
宝_爸 2015-04-10
  • 打赏
  • 举报
回复
引用 4 楼 flyingsheep_mrl 的回复:
[quote=引用 2 楼 findcaiyzh 的回复:] 看看这个讨论有帮助没 http://stackoverflow.com/questions/11155520/win32-networkadapterconfiguration-net-4-setting-static-ip-works-on-xp-but-not
问题原来在这里啊!“works on XP but not on Windows 7”!但在WIN7下有什么解决方案啊?[/quote] 他自己不是找到解决方案了吗?
huanghua 2015-04-08
  • 打赏
  • 举报
回复
代码没问题 ,4.5以上框架的,想像其它方面的原因吧。
OldEagle33032 2015-04-08
  • 打赏
  • 举报
回复
你这个是内网还容易点 我的改外网头痛死了

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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