c# 修改ip地址等等 本帖最后由 icebear2 于 2009-07-13 16:27:52 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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[] { "10.22.21.111","192.168.10.9" }; 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[] { "10.22.21.1","192.168.10.1"}; outPar = mo.InvokeMethod( "SetGateways", inPar, null ); //设置DNS inPar = mo.GetMethodParameters("SetDNSServerSearchOrder"); inPar["DNSServerSearchOrder"] = new string[] {"179.32.42.4","179.32.42.5"}; outPar = mo.InvokeMethod( "SetDNSServerSearchOrder" ,inPar,null); break; } } 楼主说不用WMI哦上面的不行吧!期待中最好有牛人自己写一个组件出来哦!好好学 不用使用WMI,下面这样也可以。//获取本地连接信息NetworkInterface[] NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface NetworkIntf in NetworkInterfaces) { if(NetworkIntf.NetworkInterfaceType ==NetworkInterfaceType.Ethernet ) comboBox1.Items.Add(NetworkIntf.Name); }//如果有网卡,默认选中第一块网卡 if (comboBox1.Items.Count > 0) { comboBox1.SelectedIndex = 0; GetNetInfor(comboBox1.SelectedItem.ToString()); } public void GetNetInfor(string strNetAdapterName) { //获取选中的网卡的IP,网关,子网掩码,DNS,备用DNS信息 NetworkInterface[] NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface NetworkIntf in NetworkInterfaces) { if (NetworkIntf.Name == strNetAdapterName) { IPInterfaceProperties IPInterfaceProperties = NetworkIntf.GetIPProperties(); //获取本机IP地址信息,子网掩码信息 UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses; if (UnicastIPAddressInformationCollection.Count > 0) { textBox1.Text = UnicastIPAddressInformationCollection[0].Address.ToString(); textBox2.Text = UnicastIPAddressInformationCollection[0].IPv4Mask.ToString(); } else { textBox1.Text = "0.0.0.0"; textBox2.Text = "0.0.0.0"; } //获取当前网关设置 GatewayIPAddressInformationCollection GWAddress = IPInterfaceProperties.GatewayAddresses; if (GWAddress.Count > 0) textBox3.Text = GWAddress[0].Address.ToString(); else textBox3.Text = "0.0.0.0"; //获取当前DNS设置 IPAddressCollection DNSAddress = IPInterfaceProperties.DnsAddresses; switch (DNSAddress.Count ) { case 1: textBox4.Text = DNSAddress[0].ToString(); textBox5.Text = "0.0.0.0"; break; case 2: textBox4.Text = DNSAddress[0].ToString(); textBox5.Text = DNSAddress[1].ToString(); break; default : textBox4.Text = "0.0.0.0"; textBox5.Text = "0.0.0.0"; break; } } } } 修改自动获取private ManagementBaseObject iObj = null; private ManagementBaseObject oObj = null; private ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); private readonly ManagementObjectCollection moc; 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); } } } public Form1() { InitializeComponent(); moc = mc.GetInstances(); } private void button3_Click(object sender, EventArgs e) { EnableDHCP(); }另外用cmd命令自动获取private void button1_Click(object sender, EventArgs e) { string a = "set address name =\"本地连接\" source = dhcp"; string b = "set dns name =\"本地连接\" source = dhcp"; string c = "set wins name =\"本地连接\" source = dhcp"; Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start(); p.StandardInput.WriteLine("netsh"); p.StandardInput.WriteLine("interface"); p.StandardInput.WriteLine("ip"); p.StandardInput.WriteLine(a.ToString()); p.StandardInput.WriteLine(b.ToString()); p.StandardInput.WriteLine(c.ToString()); p.StandardInput.WriteLine("exit"); p.StandardInput.WriteLine("exit"); p.Close(); } 这句有错误moc = mc.GetInstances();未处理 System.Management.ManagementException Message="找不到 " Source="System.Management" StackTrace: 在 System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode) 在 System.Management.ManagementObject.Initialize(Boolean getObject) 在 System.Management.ManagementBaseObject.get_wbemObject() 在 System.Management.ManagementBaseObject.get_ClassName() 在 System.Management.ManagementClass.GetInstances(EnumerationOptions options) 在 System.Management.ManagementClass.GetInstances() 在 ChangeIp.Form1..ctor() 位置 d:\My Documents\ChangeIp\ChangeIp\ChangeIp\Form1.cs:行号 21 在 ChangeIp.Program.Main() 位置 d:\My Documents\ChangeIp\ChangeIp\ChangeIp\Program.cs:行号 18 在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) 在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 在 System.Threading.ThreadHelper.ThreadStart() InnerException: 用批的话就这样@echo offset /p a=请输入所要更改的IPset /p b=请输入网关netsh int ip set address %a% static %b% 1pause>nul 。net 程序的运行、编译 如何程序控制标签打印机 如何用正则表达式取得<div class='a'></div>内的值? [C# WinForm]小窗口,大图片? C# Primer Plus中文版的附录 问个问题 高手帮忙看看 在c#里又类型BCB中DBNavigator功能的控件吗 有关数据库字段和窗体控件 服务器端装了Sql Server,客户端通过ado.net与服务器端连接,客户端还要不要装sql server 的客户端? c#和vc.net有什么关系吗? oracle插入语句总提示缺少豆号 给看哈怎么回事 验证码识别为字符
{
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[] { "10.22.21.111","192.168.10.9" };
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[] { "10.22.21.1","192.168.10.1"};
outPar = mo.InvokeMethod( "SetGateways", inPar, null );
//设置DNS
inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
inPar["DNSServerSearchOrder"] = new string[] {"179.32.42.4","179.32.42.5"};
outPar = mo.InvokeMethod( "SetDNSServerSearchOrder" ,inPar,null);
break;
}
}
//获取本地连接信息
NetworkInterface[] NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface NetworkIntf in NetworkInterfaces)
{
if(NetworkIntf.NetworkInterfaceType ==NetworkInterfaceType.Ethernet )
comboBox1.Items.Add(NetworkIntf.Name);
}
//如果有网卡,默认选中第一块网卡
if (comboBox1.Items.Count > 0)
{
comboBox1.SelectedIndex = 0;
GetNetInfor(comboBox1.SelectedItem.ToString());
} public void GetNetInfor(string strNetAdapterName)
{
//获取选中的网卡的IP,网关,子网掩码,DNS,备用DNS信息
NetworkInterface[] NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface NetworkIntf in NetworkInterfaces)
{
if (NetworkIntf.Name == strNetAdapterName)
{
IPInterfaceProperties IPInterfaceProperties = NetworkIntf.GetIPProperties();
//获取本机IP地址信息,子网掩码信息
UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses;
if (UnicastIPAddressInformationCollection.Count > 0)
{
textBox1.Text = UnicastIPAddressInformationCollection[0].Address.ToString();
textBox2.Text = UnicastIPAddressInformationCollection[0].IPv4Mask.ToString();
}
else
{
textBox1.Text = "0.0.0.0";
textBox2.Text = "0.0.0.0";
}
//获取当前网关设置
GatewayIPAddressInformationCollection GWAddress = IPInterfaceProperties.GatewayAddresses;
if (GWAddress.Count > 0)
textBox3.Text = GWAddress[0].Address.ToString();
else
textBox3.Text = "0.0.0.0";
//获取当前DNS设置
IPAddressCollection DNSAddress = IPInterfaceProperties.DnsAddresses;
switch (DNSAddress.Count )
{
case 1:
textBox4.Text = DNSAddress[0].ToString();
textBox5.Text = "0.0.0.0";
break;
case 2:
textBox4.Text = DNSAddress[0].ToString();
textBox5.Text = DNSAddress[1].ToString();
break;
default :
textBox4.Text = "0.0.0.0";
textBox5.Text = "0.0.0.0";
break;
}
}
}
}
自动获取
private ManagementBaseObject iObj = null;
private ManagementBaseObject oObj = null;
private ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
private readonly ManagementObjectCollection moc;
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);
}
}
}
public Form1()
{
InitializeComponent();
moc = mc.GetInstances();
}
private void button3_Click(object sender, EventArgs e)
{
EnableDHCP();
}另外用cmd命令自动获取private void button1_Click(object sender, EventArgs e)
{
string a = "set address name =\"本地连接\" source = dhcp";
string b = "set dns name =\"本地连接\" source = dhcp";
string c = "set wins name =\"本地连接\" source = dhcp"; Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start(); p.StandardInput.WriteLine("netsh");
p.StandardInput.WriteLine("interface");
p.StandardInput.WriteLine("ip");
p.StandardInput.WriteLine(a.ToString());
p.StandardInput.WriteLine(b.ToString());
p.StandardInput.WriteLine(c.ToString());
p.StandardInput.WriteLine("exit");
p.StandardInput.WriteLine("exit");
p.Close();
}
Message="找不到 "
Source="System.Management"
StackTrace:
在 System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
在 System.Management.ManagementObject.Initialize(Boolean getObject)
在 System.Management.ManagementBaseObject.get_wbemObject()
在 System.Management.ManagementBaseObject.get_ClassName()
在 System.Management.ManagementClass.GetInstances(EnumerationOptions options)
在 System.Management.ManagementClass.GetInstances()
在 ChangeIp.Form1..ctor() 位置 d:\My Documents\ChangeIp\ChangeIp\ChangeIp\Form1.cs:行号 21
在 ChangeIp.Program.Main() 位置 d:\My Documents\ChangeIp\ChangeIp\ChangeIp\Program.cs:行号 18
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:
@echo off
set /p a=请输入所要更改的IP
set /p b=请输入网关
netsh int ip set address %a% static %b% 1
pause>nul