如何编程设置本机IP
主 题: [网络编程问题] 如何编程设置本机IP
作 者: armylau (冯.城.褐.兰)
等 级: ^^
信 誉 值: 100
所属论坛: .NET技术 C#
问题点数: 100
回复次数: 2
发表时间: 2003-09-20 11:54:34
本人PC由于特殊原因需经常修改IP和网关, 想写一个程序方便完成, 请问如如何:
- 编程修改本机IP, 网关, 子网掩码, DNS等...(是否需要调用API?)
- 如何禁用和启动网络, 以完成其刷新(又是调用API吧,具体是什么?)
谢谢!我会尽快加分
回复人: chinchy(人民需要人民币) ( 两星(中级)) 信誉:140 2003-09-20 12:13:00 得分:0
If you like, low-level IP-Helper API:
http://msdn.microsoft.com/library/en-us/tcpip/iphpport_7vz9.asp
PInvoke with C# (no IP change impl.?)http://www.gotdotnet.com/team/p2p/
http://www.gotdotnet.com/userfiles/herveyw/netsamples.zip
WMI:
Win32_NetworkAdapterConfiguration:http://msdn.microsoft.com/library/en-us/wmisdk/r_32hard4_6oq6.asp
Methods: EnableStatic + EnableDHCPhttp://msdn.microsoft.com/library/en-us/wmisdk/r_32hard4_0ujy.asp
http://msdn.microsoft.com/library/en-us/wmisdk/r_32hard4_27u6.asp
Changeip VB-Scripts:http://desktopengineer.com/index.php?topic=0080WMI
http://cwashington.netreach.net/depo/view.asp?Index=628
VERY simplified WMI sample:
// ===============================================================================
using System;
using System.Management;
using System.Threading;
namespace WmiIpChanger
{
class IpChanger
{
[MTAThread]
static void Main(string[] args)
{
ReportIP();
// SwitchToDHCP();
SwitchToStatic();
Thread.Sleep(5000);
ReportIP();
Console.WriteLine("end.");
}
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[] { "192.168.1.1" };
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}' 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);
}
}
}
}
回复人: chinawn(chinawn) ( 二级(初级)) 信誉:100 2003-09-20 12:49:00 得分:0
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=%23EwffAXACHA.2308%40tkmsftngp02
Viewing message #EwffAXACHA.2308@tkmsftngp02
DynamicPDF Generator.NET?
Total output control! - New v1.6 Web Invoices, Schedules & Reports.? dynamicpdf.com
C# TraceListener Article?
Need a free C# TraceListener? Use Reflection to leverage .NET? www.codeproject.com
C# tutorial and resources?
AspRelated.com offers free C# tutorials, resources, articles? www.asprelated.com
From: NETMaster (spam.netmaster@swissonline.ch)
Subject: Re: WMI or How to change my IP address
View: Complete Thread (6 articles)
Original Format
Newsgroups: microsoft.public.dotnet.framework
Date: 2002-05-22 02:09:48 PST
If you like, low-level IP-Helper API: http://msdn.microsoft.com/library/en-us/tcpip/iphpport_7vz9.asp
PInvoke with C# (no IP change impl.?) http://www.gotdotnet.com/team/p2p/ http://www.gotdotnet.com/userfiles/herveyw/netsamples.zip
WMI: Win32_NetworkAdapterConfiguration: http://msdn.microsoft.com/library/en-us/wmisdk/r_32hard4_6oq6.asp
Methods: EnableStatic + EnableDHCP http://msdn.microsoft.com/library/en-us/wmisdk/r_32hard4_0ujy.asp http://msdn.microsoft.com/library/en-us/wmisdk/r_32hard4_27u6.asp
Changeip VB-Scripts: http://desktopengineer.com/index.php?topic=0080WMI http://cwashington.netreach.net/depo/view.asp?Index=628
VERY simplified WMI sample:
// ===============================================================================
using System;
using System.Management;
using System.Threading;
namespace WmiIpChanger
{
class IpChanger
{
[MTAThread]
static void Main(string[] args)
{
ReportIP();
// SwitchToDHCP();
SwitchToStatic();
Thread.Sleep( 5000 );
ReportIP();
Console.WriteLine( "end." );
}
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[] { "192.168.1.1" };
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}' 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 );
}
}
}
}
// ===============================================================================
WARNING: do MUCH more error checking, multiple NIC tests, timing!...
use all at at your own risk!
--
NETMaster (Thomas Scheidegger)
http://www.cetus-links.org/oo_csharp.html
"Marco Scheel" atWork@Visual-eVolution.de wrote in message news:14765441.1022081866849.JavaMail.SYSTEM@oscar...
i'm looking for a way to change the local ip address auf my notebook. i'd like to write some code that can change my configuration from static ip to an dhcp configuration. looked at System.Managment, but i've got no idea how this wmi thing works.